gpt4 book ai didi

c# - 反序列化 KeyValuePair Json.Net

转载 作者:太空狗 更新时间:2023-10-29 21:34:55 25 4
gpt4 key购买 nike

我需要反序列化 string像这样

{  "example": {    "id": "12345",    "name": "blabla"  }}

into a KeyValuePair<string, string> or something similiar.

I tried:

var pair = JsonConvert.DeserializeObject<KeyValuePair<string, string>>(d["example"].ToString()); 

(d["example"] 返回如上所示的 json 字符串)

结果是空的 KeyValuePair<string, string> .

有什么办法可以解决吗?

最佳答案

string json = 
@"{
""example"": {
""id"": ""12345"",
""name"": ""blabla""
}
}";

var jobj = JObject.Parse(json);
var dict = jobj["example"]
.Children().Cast<JProperty>()
.ToDictionary(x => x.Name, x => (string)x.Value);

var dict = jobj["example"].ToObject<Dictionary<string, string>>();

关于c# - 反序列化 KeyValuePair<string, string> Json.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13296824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com