Chris Lombardi
Chris Lombardi

Reputation: 891

RESTful service and JSON array

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);
    }

Upvotes: 3

Views: 2410

Answers (3)

narekye
narekye

Reputation: 114

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);

Upvotes: 1

Aaron Roberts
Aaron Roberts

Reputation: 1372

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" }]

Upvotes: 0

L.B
L.B

Reputation: 116098

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();

Upvotes: 1

Related Questions