Furkan Gözükara
Furkan Gözükara

Reputation: 23850

How to parse this Json with C# using json.net

Ok this is the example json string i want to format

What i want is simple but i have no idea how to achieve it

I have successfully installed json.net

I load json string as follow: JObject myJsonNetObject = JObject.Parse(jsonCode);

Q1: I want to navigate between each element of "result" how do i do that with json.net.linq ?

Q2: I want to get certain objects parameters with giving their names

something like

    foreach (var vrElement in myJsonNetObject.select(???))
    {
       string commentid= vrElement.CommentId;
//or
       string commentid= vrElement["CommentId"]; ??
    }

Thank you very much for answers

C# WPF, .net 4.5, Json.NET 6.0.4

    {
   "RenderedTime":"2014-08-28 03:00:27",
   "succeded":true,
   "result":[
      {
         "CommentId":79107,
         "ContentTypeId":8,
         "ContentId":0,
         "ProductId":"110015198",
         "ProductName":"LG 32LB652V DVB-S2/T2/C 3D FHD WEBOS SMART LED LCD TV + 2 GÖZLÜK",
         "FirstLastName":"ERGUN GOKCUOGLU",
         "Gender":1,
         "TimeUsed":"1-3 Ay",
         "City":"İzmir",
         "BirthRange":"36-50 arası",
         "Comment":"Cihazı kullanmaya başladım ürün fena değil görüntü kalitesi iyi içerisinden çıkan 3d pasif gözlük 310 modeli yani çok iyi değil kumanda akıllı kumanda olmadığı için büyük ve işlevi çok iyi değil ama paranın karşılığı bir ürün alınabilir.",
         "Eof":false,
         "NameVisible":false,
         "Onay":1,
         "ContentType":"comment",
         "GoodCount":0,
         "BadCount":0,
         "Id":{
            "Type":0,
            "Id":null,
            "IdGuid":null,
            "HasValue":false
         },
         "ExtendedProperties":[

         ]
      },
      {
         "CommentId":78954,
         "ContentTypeId":8,
         "ContentId":0,
         "ProductId":"110015198",
         "ProductName":"LG 32LB652V DVB-S2/T2/C 3D FHD WEBOS SMART LED LCD TV + 2 GÖZLÜK",
         "FirstLastName":"volkan yıldırım",
         "Gender":1,
         "TimeUsed":"1-3 Ay",
         "City":"Rize",
         "BirthRange":"18-25 arası",
         "Comment":"Dün aldım kurcaladım gayet iyi çok menmun kaldım interneti smart tvsi çözünürlük 10 numara bi kalite bu fiyat çok iyi 81 ekran için en ideal cihaz",
         "Eof":false,
         "NameVisible":false,
         "Onay":1,
         "ContentType":"comment",
         "GoodCount":0,
         "BadCount":0,
         "Id":{
            "Type":0,
            "Id":null,
            "IdGuid":null,
            "HasValue":false
         },
         "ExtendedProperties":[

         ]
      },
      {
         "CommentId":78789,
         "ContentTypeId":8,
         "ContentId":0,
         "ProductId":"110015198",
         "ProductName":"LG 32LB652V DVB-S2/T2/C 3D FHD WEBOS SMART LED LCD TV + 2 GÖZLÜK",
         "FirstLastName":"ATAKAN ÇELİK",
         "Gender":1,
         "TimeUsed":"Seçiniz",
         "City":"Seçiniz",
         "BirthRange":"Seçiniz",
         "Comment":"ürün çok güzel ve sık bir tasarıma sahip güzel bir akıllı televizyon lg yapıyo arkadaşlar ve kendilerini devamlı geliştiriyorlar şimdi webos özelliğide koymuşlar çok çok güzel",
         "Eof":false,
         "NameVisible":false,
         "Onay":1,
         "ContentType":"comment",
         "GoodCount":0,
         "BadCount":0,
         "Id":{
            "Type":0,
            "Id":null,
            "IdGuid":null,
            "HasValue":false
         },
         "ExtendedProperties":[

         ]
      },
      {
         "ContentType":"recommendation",
         "Eof":false,
         "Id":{
            "Type":0,
            "Id":null,
            "IdGuid":null,
            "HasValue":false
         },
         "ExtendedProperties":[

         ]
      },
      {
         "CommentId":76687,
         "ContentTypeId":8,
         "ContentId":0,
         "ProductId":"110015198",
         "ProductName":"LG 32LB652V DVB-S2/T2/C 3D FHD WEBOS SMART LED LCD TV + 2 GÖZLÜK",
         "FirstLastName":"ATİLLA KASIMOĞLU",
         "Gender":1,
         "TimeUsed":"1-5 Ay",
         "City":"Adıyaman",
         "BirthRange":"36-50 arası",
         "Comment":"ürünün kasasi o kadar iyi olmasada islemcinin biraz düsük olmasi bu led tv eksik yönleri ama 500 mhz olmasi webos olmasi da avantajlarindan alinacak tvlerden biri",
         "Eof":true,
         "NameVisible":true,
         "Onay":1,
         "ContentType":"comment",
         "GoodCount":0,
         "BadCount":0,
         "Id":{
            "Type":0,
            "Id":null,
            "IdGuid":null,
            "HasValue":false
         },
         "ExtendedProperties":[

         ]
      }
   ],
   "error":null
}

Upvotes: 1

Views: 993

Answers (1)

Brian Rogers
Brian Rogers

Reputation: 129787

You're not that far off. Try something like this:

JObject myJsonNetObject = JObject.Parse(jsonCode);

foreach (JObject item in myJsonNetObject["result"])
{
    if (item["ContentType"].ToString() == "comment")
    {
        Console.WriteLine("CommentId: " + item["CommentId"]);
        Console.WriteLine("City: " + item["City"]);
        // etc..
    }
}

Demo: https://dotnetfiddle.net/1Zd73d

Upvotes: 2

Related Questions