如何使用Json.net用C#解析这个Json

本文关键字:Json 何使用 net | 更新日期: 2023-09-27 18:19:37

好的,这是我想要格式化的示例json字符串

我想要的很简单,但我不知道如何实现

我已成功安装json.net

我加载json字符串如下:JObject myJsonNetObject = JObject.Parse(jsonCode);

Q1:我想在"result"的每个元素之间导航,如何使用json.net.linq

Q2:我想通过给出某些对象的名称来获得它们的参数

类似的东西

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

非常感谢您的回答

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
}

如何使用Json.net用C#解析这个Json

你离这里不远了。试试这样的东西:

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..
    }
}

演示:https://dotnetfiddle.net/1Zd73d