glarkou
glarkou

Reputation: 7101

Parse JSON object in C#

I use JSON.NET and I would like to parse the following object which I get from a WebService. Can someone post an example on how to do that?

@"{""MessageType"":0,
   ""Message"":""Success"",
   ""Value"":[
              {""listId"":1,
               ""listName"":""DemoList"",
               ""itemInList"":[
                    {
                     ""fromDate"":""\/Date(1228946400000)\/"",
                     ""fromLocation"":null,
                     ""toLocation"":null,
                     ""originalRequest"":""water"",
                     ""creationDate"":""\/Date(1339448400000)\/"",
                     ""typeId"":1
                    },
                    {
                     ""fromDate"":null,
                     ""fromLocation"":null,
                     ""toLocation"":null,
                     ""originalRequest"":""gala"",
                     ""creationDate"":""\/Date(1304370000000)\/"",
                     ""typeId"":1
                    }
              ]}
    ]}"

JSON Object

{
  "MessageType":0,
  "Message":"UserLists",
  "Value":
          [
            {
              "listId":1,
              "listName":"DemoList",
              "itemInList" 
                    [
                      {
                         "fromDate":"\/Date(1228946400000)\/",
                         "fromLocation":null,
                         "toLocation":null,
                         "originalRequest":"water",
                         "creationDate":"\/Date(1339448400000)\/",
                         "typeId":1
                      },
                      {
                         "fromDate":null,
                         "fromLocation":null,
                         "toLocation":null,
                         "originalRequest":"gala",
                         "creationDate":"\/Date(1304370000000)\/",
                         "typeId":1
                       }
                  ],
                  "numberOfItems":2
              }
          ]
     }

Thanks.

Upvotes: 1

Views: 6655

Answers (2)

ashish
ashish

Reputation: 1

Please read the below link for parsi in metro style application. http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh770287.aspx

Upvotes: 0

Dozer
Dozer

Reputation: 5225

You need to create some entity like this:

public class Entity
{
    public int MessageType { get; set; }
    public string Message { get; set; }
    public List<EntityValue> Value { get; set; }
}

public class EntityValue
{
    public int listId { get; set; }
    public string listName { get; set; }
    public List<ItemInList> itemInList { get; set; }
}

public class ItemInList
{
    public DateTime? fromDate { get; set; }
    public string fromLocation { get; set; }
    public string toLocation { get; set; }
    public string originalRequest { get; set; }
    public DateTime creationDate { get; set; }
    public int typeId { get; set; }
}

The entity must has the same structure like the json data. And you can call the Method:

JsonConvert.DeserializeObject<Entity>(json);

If it has any exception,you need to adjust the entities until it works.

Upvotes: 7

Related Questions