我需要在C#中解析JSON的帮助。有我的JSON字符串,我正试图分析和处理。我不想创建一个类实例化对象,因为可以有更多的呼叫有很多返回的对象类型 - 锦标赛,团队,用户等C#JSON反序列化字典异常
{
"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);
}
}
有这个问题解析:
类型的未处理的异常“Newtonsoft.Json.JsonSerializationEx ception'发生在 Newtonsoft.Json.dll
附加信息:无法反序列化当前的JSON对象 (例如,因为这个类型需要一个JSON数组(例如[1,2,...,0,{“name”:“value”}),因此类型为 'System.Collections.Generic.Dictionary`2 [System.String,System.String] []' ' 3])正确地反序列化 。
要修复此错误,请将JSON更改为JSON数组(例如 [1,2,3])或更改反序列化类型,以使其成为正常的.NET 类型(例如,不是整数类型的基本类型,而不是类似数组或列表的集合类型 ),它们可以从JSON对象反序列化。 也可以将JsonObjectAttribute添加到该类型中,以强制它从一个JSON对象反序列化为 。
路径 '响应',1号线,位置12
感谢您的帮助! :)
你的JSON中绝对没有数组。 –