Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
like image 253
Chris Lombardi Avatar asked Apr 05 '26 19:04

Chris Lombardi


2 Answers

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();
like image 162
L.B Avatar answered Apr 08 '26 14:04

L.B


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);
like image 41
narekye Avatar answered Apr 08 '26 13:04

narekye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!