简体   繁体   English

在不创建反序列化类的情况下访问 json 中的某个变量

[英]Accessing a certain variable inside a json without creating a deserialization class

I currently have a json string that looks like this我目前有一个看起来像这样的 json 字符串

{ 
   "strA":"somestr",
   "strB":{ 
      "strB":"String B",
      "strC":"String C",
      "sections":[ 
         { 
            "strD":"...",
            "str$":"...",
            "myArray":[ 
               { 
                  "name":"xxxx",
                  "value":"xxxx"
               }
            ]
         }
      ]
   }
}

Now the content of this json could change however strB will always be where it is.现在这个 json 的内容可能会改变,但是strB将始终保持strB The value of strB can always change. strB的值总是可以改变的。 I would like to get the value of strB as string.我想将strB的值作为字符串获取。 I tried doing this我试过这样做

var info = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(str);
string strB = info["strB"].ToString();

but that seems to be wrong .但这似乎是错误的。 Any idea on how I can extract strB value from this json as a string without creating a deserialization class .关于如何从这个 json 中提取 strB 值作为字符串而不创建反序列化类的任何想法。

You can use JObject and run linq on it.您可以使用JObject并在其上运行 linq。

 JObject json = JObject.Parse(jsonstring);
 // access the value like this
 json["strB"]

Find some very good example: https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm找一些很好的例子: https : //www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM