簡體   English   中英

如何將具有不同值類型的JSON字符串轉換為C#詞典?

[英]How to convert JSON string with different value types to C# Dictionary?

因此,我要轉換此JSON字符串:

{
 "Posts" : 
    [
     {
       "Title" : "just a string",
       "Id" : "231"
     }, {
       "Title" : "Another string as title",
       "Id": "41"
     }
    ],
  "anotherKey" : "third string"
}

到C#字典,但是此代碼(使用Newtonsoft.Json包;其中jsonString是存儲該JSON字符串的變量):

Dictionary<string,Array> test1 = JsonConvert.DeserializeObject<Dictionary<string,Array>>(jsonString);
Console.WriteLine(test1.Count);

返回錯誤:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Unable to find a constructor to use for type System.Array. Path 'Posts', line 1, position 10.

是另一種方法如何將此字符串轉換為Dictionary?

第二個條目不是數組,而是單個字符串。

嘗試反序列化為Dictionary<string,object>Dictionary<string,dynamic>以讀取所有值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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