[英]Accessing a certain variable inside a json without creating a deserialization class
我目前有一個看起來像這樣的 json 字符串
{
"strA":"somestr",
"strB":{
"strB":"String B",
"strC":"String C",
"sections":[
{
"strD":"...",
"str$":"...",
"myArray":[
{
"name":"xxxx",
"value":"xxxx"
}
]
}
]
}
}
現在這個 json 的內容可能會改變,但是strB
將始終保持strB
。 strB
的值總是可以改變的。 我想將strB
的值作為字符串獲取。 我試過這樣做
var info = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(str);
string strB = info["strB"].ToString();
但這似乎是錯誤的。 關於如何從這個 json 中提取 strB 值作為字符串而不創建反序列化類的任何想法。
您可以使用JObject
並在其上運行 linq。
JObject json = JObject.Parse(jsonstring);
// access the value like this
json["strB"]
找一些很好的例子: https : //www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.