使用Json.Net將JSON對象反序列化為動態對象
Json.Net能否將JSON對象反序列化為動態對象?例如:
<code class="language-csharp">dynamic jsonResponse = JsonConvert.DeserializeObject(json); Console.WriteLine(jsonResponse.message);</code>
答案:
是的,Json.Net可以實現這一點:
<code class="language-csharp">dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}"); Console.WriteLine(d.number); Console.WriteLine(d.str); Console.WriteLine(d.array.Count);</code>
輸出:
<code>1000 string 6</code>
請參考Json.Net的LINQ to JSON文檔。此外,請考慮使用JObject.Parse
和JArray.Parse
。
以上是JSON.NET可以將JSON供電到動態對象嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!