Home > DeveloperSection > Forums > Parsing JSON in C# issue
Ashish Pandey

Total Post:128

Points:898
Posted on    April-01-2014 7:08 AM

 C# C# 
Ratings:


 1 Reply(s)
 1428  View(s)
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) ) .



Pravesh Singh

Total Post:411

Points:2881
Posted on    April-11-2014 12:03 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick