gpt4 book ai didi

c# - Json.NET 未能将 Json 反序列化为仅字段类

转载 作者:行者123 更新时间:2023-11-30 21:48:21 24 4
gpt4 key购买 nike

我想用 Newtonsoft Json.NET 解析一段 JSON

JSON:

{

"USER":{
"result_id":"0",
"result_description":"NET Connections",
"cmlog_username":[
"8118236834",
"8118236834",
"8118236834"
],
"caller_id":[
"14cc20f7b05f",
"14cc20f7b05f",
"14cc20f7b05f"
]
}

}

 public class USER
{
public string result_id;
public string result_description;
public string[] cmlog_username;
public string[] caller_id;
}//USER

我用下面的代码转换它,但所有的属性值都是 NULL

USER con = JsonConvert.DeserializeObject<USER>(msg);

最佳答案

您的反序列化类不正确。将您的 JSON 放入 json2csharp.com产生:

public class USER
{
public string result_id { get; set; }
public string result_description { get; set; }
public List<string> cmlog_username { get; set; }
public List<string> caller_id { get; set; }
}

public class RootObject
{
public USER USER { get; set; }
}

所以你需要做:

User con = JsonConvert.DeserializeObject<RootObject>(msg);

您的 JSON 对象不是 USER,它是一个包含 USER 的对象。

关于c# - Json.NET 未能将 Json 反序列化为仅字段类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37728533/

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