Parsing JSON in C# issue

Total Post:128

Points:898
C# 
C#
 2101  View(s)
Ratings:
Rate this:

I have a problem parsing the json, and I hope someone could help.

Here's a JSON response I get from the service

{
    "name":"UPDATE_QUEUE",
    "args":[
        {
            "message":[
                {
                    "service_id":1,
                    "entered":"01:00",
                    "polaziste":"Tibljaska cesta 11, Rijeka",
                    "phone_number":"0992173439",
                    "destinacija":"Zabica 1, Rijeka",
                    "order":1,
                    "passanger_count":1,
                    "order_arival":"01:00"
                },
                {
                    "service_id":2,
                    "entered":"01:00",
                    "polaziste":"Cavle 15, Cavle",
                    "phone_number":"08001234",
                    "destinacija":"Centar 1, Matulji",
                    "order":1,
                    "passanger_count":3,
                    "order_arival":"01:00"
                }
            ]
        }
    ]
}

And here's my Class for the JSON object

    class DispatchQueueItem
    {
        public RootObject RootObject { get; set; }
    }
    public class Message
    {
        public int service_id { get; set; }
        public string entered { get; set; }
        public string polaziste { get; set; }
        public string phone_number { get; set; }
        public string destinacija { get; set; }
        public int order { get; set; }
        public int passanger_count { get; set; }
        public string order_arival { get; set; }
    }
    public class Arg
    {
        public List<Message> message { get; set; }
    }
    public class RootObject
    {
        public string name { get; set; }
        public List<Arg> args { get; set; }
    }

And here's my call

API.jsonObjects.DispatchQueueItem items

 = JsonConvert.DeserializeObject<API.jsonObjects.DispatchQueueItem>(data.MessageText);

I am using Newtonsoft JSON for c# .NET

When i try to call items.RootObject.name i don't get anything, not even trigger for the event ( for example messageBox.Show(items.RootObject.name) ) .

  1. Post:412

    Points:2888
    Re: Parsing JSON in C# issue

    Hi Ashish,


    You are deserializing to the wrong type. You should call

    JsonConvert.DeserializeObject<API.jsonObjects.RootObject>(data.MessageText);

    The RootObject class matches the JSON which you are deserializing.

Answer