簡體   English   中英

在不創建反序列化類的情況下訪問 json 中的某個變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM