C#JSON反序列化字典异常
我需要C#中JSON解析的帮助。这是我试图解析和使用的JSON字符串。我不想创建一个类来实例化该对象,因为可能会有更多调用返回大量对象类型-锦标赛、团队、用户等C#JSON反序列化字典异常,c#,dictionary,serialization,json.net,C#,Dictionary,Serialization,Json.net,我需要C#中JSON解析的帮助。这是我试图解析和使用的JSON字符串。我不想创建一个类来实例化该对象,因为可能会有更多调用返回大量对象类型-锦标赛、团队、用户等 { "response":{ "2":{ "tournament_id":2, "created_at":{ "date":"2015-11-09 21:01:06", "timezone_type":
{
"response":{
"2":{
"tournament_id":2,
"created_at":{
"date":"2015-11-09 21:01:06",
"timezone_type":3,
"timezone":"Europe/Prague"
},
"creator_id":1,
"name":"Tournament Test #1",
"desc":"...!",
"state":0,
"is_visible":1
},
"3":{
"tournament_id":3,
"created_at":{
"date":"2015-11-09 21:01:06",
"timezone_type":3,
"timezone":"Europe/Prague"
},
"creator_id":1,
"name":"Tournament Test #2",
"desc":"...",
"state":1,
"is_visible":1
}
},
"error":false
}
我使用JSON.net库解析JSON字符串,这是我在程序中使用的C代码:
public class API
{
private WebClient client;
protected string auth_key = "xxx";
protected string base_url = "http://127.0.0.1/tournaments_api/www/";
private string endpoint_url = "";
private string url_params = "";
public string url_data;
public Dictionary<string, string>[] data;
public bool success = false;
public string errorMessage = "";
public int errorCode = 0;
public API()
{
this.client = new WebClient();
}
private void Request()
{
string url = this.base_url + this.endpoint_url + "/" + this.auth_key + "?" + this.url_params;
this.url_data = this.client.DownloadString(url);
Console.WriteLine(this.url_data);
this.data = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(this.url_data);
}
}
公共类API
{
私人网络客户端;
受保护的字符串auth_key=“xxx”;
受保护的字符串base_url=”http://127.0.0.1/tournaments_api/www/";
私有字符串端点_url=“”;
私有字符串url_params=“”;
公共字符串url_数据;
公共字典[]数据;
公共布尔成功=错误;
公共字符串errorMessage=“”;
公共内部错误代码=0;
公共API()
{
this.client=新的WebClient();
}
私有无效请求()
{
字符串url=this.base\u url+this.endpoint\u url+“/”+this.auth\u key+“?”+this.url\u参数;
this.url_data=this.client.DownloadString(url);
Console.WriteLine(这个.url\u数据);
this.data=JsonConvert.DeserializeObject(this.url\u数据);
}
}
解析时存在以下问题:
类型的未处理异常
在中发生“Newtonsoft.Json.JsonSerializationException”
Newtonsoft.Json.dll
其他信息:无法反序列化当前JSON对象
(例如,{“名称”:“值”})转换为类型
'System.Collections.Generic.Dictionary'2[System.String,System.String][]
因为该类型需要一个JSON数组(例如[1,2,3])来反序列化
没错
要修复此错误,请将JSON更改为JSON数组(例如。
[1,2,3])或更改反序列化类型,使其成为普通的.NET
类型(例如,不是integer之类的基元类型,也不是集合类型
类似于数组或列表),可以从JSON对象反序列化。
还可以将JsonObjectAttribute添加到类型以强制其
从JSON对象反序列化
路径“响应”,第1行,位置12
谢谢你的帮助!:) 您的JSON是一个对象,在C中可以反序列化为
字典
。但是,您尝试将其反序列化为数组,而绝对没有数组
您需要将此更改为:
public Dictionary<string, object>[] data;
// ...
JsonConvert.DeserializeObject<Dictionary<string, object>>(this.url_data);
然后,您将能够像处理动态对象一样处理它:
var err = data.error;
同时,创建一个类来描述此模型并用于反序列化此JSON对我来说更合适。除了Yeldar Kurmangaliyev的答案外,我还可以使用JSON.NET库的内置LINQ to JSON支持:
JObject jObject = JObject.Parse("-- JSON STRING --");
JToken response = jObject["response"];
bool error = jObject["error"].Value<bool>();
JObject-JObject=JObject.Parse(“--JSON字符串--”);
JToken response=jObject[“response”];
bool error=jObject[“error”].Value();
有很多扩展方法可以轻松解析json字符串。json中绝对没有数组。
JObject jObject = JObject.Parse("-- JSON STRING --");
JToken response = jObject["response"];
bool error = jObject["error"].Value<bool>();