3

I have a .NET Core 1.x Web API project and I am trying to accept an array on HTTP post, but I have been unable to get this working. I have validated the following JSON

    {
    "LogEntry": [{
        "a": 1238976,
        "b": "test",
        "c": "sub test",
        "d": "some syb system comp",
        "e": 1234,
        "f": "my category",
        "g": "my event name",
        "h": "my sub event",
        "i": "user def 1",
        "j": "7/22/2008 12:11:04 PM",
        "k": 45,
        "l": 65,
        "n": "Chris",
        "o": "C:\\temp\\",
        "p": 1234567890,
        "q": 84,
        "r": "eeeee stuff",
        "s": "ddddd stuff",
        "t": 90210
    }]
}

I have a model class so I need each array item to be added to a list of the model type. This is where I am stuck. I have only been able to get this working with a single entry not in an array. My C# for that scenario is:

    [HttpPost]
    public string Post([FromBody] JObject test)
    {

     var result = JsonConvert.DeserializeObject<EventManagerLogEntry>(test.ToString());

        return "wootwoot";
    }

Any direction on how I can loop through each array in my jObject and have it added to a list of type would be very helpful.

Class Definition

public class EventManagerLogEntry
    {

        public int a { get; set; }
        public string b { get; set; }
        public string c { get; set; }
        public string d { get; set; }
        public int e { get; set; }
        public string f { get; set; }
        public string g { get; set; }
        public string h { get; set; }
        public string i { get; set; }
        public string j { get; set; }
        public int k { get; set; }            
        public int l { get; set; }
        public string m { get; set; }
        public string n { get; set; }            
        public int o { get; set; }
        public int p { get; set; }
        public string q { get; set; }
        public string r { get; set; }          
        public int s { get; set; }

    }

UPDATE I tried several different methods and this seems to be working for me.

    [HttpPost]
    public HttpResponseMessage Post([FromBody] JArray test)
    {

        var list = JsonConvert.DeserializeObject<List<EventManagerLogEntry>>(test.ToString());


        foreach (EventManagerLogEntry x in list)
        {

            //_context.EventManagerLogEntry.Attach(x);
            //_context.SaveChanges();
        }

        return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
    }
3
  • Have you just tried it ? var resulListt = JsonConvert.DeserializeObject<List<EventManagerLogEntry>>(test.ToString()); Commented Sep 13, 2017 at 16:41
  • Please, provide the class definition of EventManagerLogEntry Commented Sep 13, 2017 at 16:44
  • @Pac0 - Yes. I received an error.Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WebApplication1.models.EventManagerLogEntry]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. Commented Sep 13, 2017 at 16:49

3 Answers 3

1

Use this model to deserialize your json

public class Log
{
    public List<Dictionary<string,string>> LogEntry { get; set; }
}



var log = JsonConvert.DeserializeObject<Log>(json);

You can also use linq

var jObj = JObject.Parse(json);

var listOfDict = jObj["LogEntry"]
                    .Select(x => x.Cast<JProperty>()
                                .ToDictionary(p => p.Name, p => p.Value))
                    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

Just change your model to this.

    public class EventManagerLogEntry
    {
        [JsonProperty("LogEntry")]
        public List<Node> LogEntries { get; set; }
    }
    public class Node 
    {
        public int a { get; set; }
        public string b { get; set; }
        public string c { get; set; }
        public string d { get; set; }
        public int e { get; set; }
        public string f { get; set; }
        public string g { get; set; }
        public string h { get; set; }
        public string i { get; set; }
        public string j { get; set; }
        public int k { get; set; }            
        public int l { get; set; }
        public string m { get; set; }
        public string n { get; set; }            
        public int o { get; set; }
        public int p { get; set; }
        public string q { get; set; }
        public string r { get; set; }          
        public int s { get; set; }
    }

And simple deserialize object.

var obj = JsonConvert.DeserializeObject<EventManagerLogEntry>(yourjson);

Comments

0

There are a couple improvements you can make to get this working. First thing is that you should not need to deserialize your json manually.

public class Value
{
    public String Name { get; set; }
}

...

[HttpPost("one")]
public void Post([FromBody] Value value)
{
    Console.WriteLine("got one");
}

[HttpPost("many")]
public void Post([FromBody] Value[] value)
{
    Console.WriteLine("got many");
}

you can use the class definition in the POST methods to automatically get the objects from the json.

Second you can do one of two things:

  1. Post only the array json to the server and use an array
  2. Create a new model that encapsulates your list as @L.B has mentioned.

Below are the json data sent to the above routes that work.

One
{ "name" : "test" }

Many
[{ "name" : "test" }]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.