我正在尝试使用Json.NET反序列化Json字符串。我对它有基本的了解,但我已经尝试了好几天,但无法使它工作,这是我的代码。(请忽略任何不必要/额外的代码,请记住,我正在根据研究后的发现尝试多种解决方案)。
public class Location
{
private string ab_;
private int c_;
public string name { get { return ab_; } set { value = ab_; } }
public int woeid { get { return c_; } set { value = c_; } }
}
public class Trend
{
private string x_, y_, z_;
private object a_, b_;
public string query { get { return x_; } set { value = x_; } }
public string name { get { return y_; } set { value = y_; } }
public object promoted_content { get { return a_; } set { value = a_; } }
public string url { get { return z_; } set { value = z_; } }
public object events { get { return b_; } set { value = b_; } }
}
public class RootObject
{
private string o, l;
private List<Location> list1;
private List<Trend> list2;
public List<Location> locations { get { return list1; } set { value = list1; } }
public string created_at { get { return o; } set { value = o; } }
public List<Trend> trends { get { return list2; } set { value = list2; } }
public string as_of { get { return l; } set { value = l; } }
}
RootObject values = JsonConvert.DeserializeObject<RootObject>(jsonString)
现在,我试图反序列化的Json字符串是这样的:
[
{
"as_of":"2012-08-31T15:31:55Z",
"trends":[
{
"query":"%23GDTHATXX",
"name":"#GDTHATXX",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%23GDTHATXX",
"events":null
},
{
"query":"%23UnMundoEnElQue",
"name":"#UnMundoEnElQue",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%23UnMundoEnElQue",
"events":null
},
{
"query":"%23%C3%A7ekicibuluyorum",
"name":"#u00e7ekicibuluyorum",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%23%C3%A7ekicibuluyorum",
"events":null
},
{
"query":"%22Darren%20Kenny%22",
"name":"Darren Kenny",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%22Darren%20Kenny%22",
"events":null
},
{
"query":"%22Richard%20Wright%22",
"name":"Richard Wright",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%22Richard%20Wright%22",
"events":null
},
{
"query":"Yossi",
"name":"Yossi",
"promoted_content":null,
"url":"http://twitter.com/search/?q=Yossi",
"events":null
},
{
"query":"Filipinas",
"name":"Filipinas",
"promoted_content":null,
"url":"http://twitter.com/search/?q=Filipinas",
"events":null
},
{
"query":"%22Javi%20Garc%C3%ADa%22",
"name":"Javi Garcu00eda",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%22Javi%20Garc%C3%ADa%22",
"events":null
},
{
"query":"%22Van%20der%20Vaart%22",
"name":"Van der Vaart",
"promoted_content":null,
"url":"http://twitter.com/search/?q=%22Van%20der%20Vaart%22",
"events":null
},
{
"query":"SSN",
"name":"SSN",
"promoted_content":null,
"url":"http://twitter.com/search/?q=SSN",
"events":null
}
],
"created_at":"2012-08-31T15:30:32Z",
"locations":[
{
"name":"Globales",
"woeid":1
}
]
}
]
无法将当前JSON数组(例如[1,2,3])反序列化为类型"Twitter.TrendParser+RootObject",因为该类型需要JSON对象(例如{"name":"value"})才能正确反序列化。
若要修复此错误,请将JSON更改为JSON对象(例如{"name":"value"}),或将反序列化的类型更改为数组或实现集合接口(例如ICollection、IList)的类型,例如可以从JSON数组反序列化的List。JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。
路径",第1行,位置1。
正如我之前所说的,我对Json格式有了基本的理解,并且我一直在尝试使用不同类型的类来反序列化,以获得相同的结果,关于问题可能是什么,有什么提示或想法吗?。除了json之外,我还坚持使用.NET 2.0,而不是第三方库。NET。
感谢您的关注。
- 编辑*
显然,即使我尝试声明List值,变量"value"也不会在实际上下文中创建,无论如何都无法解决这个问题。
JSON是一个包含一个对象的数组。
这意味着JSON.NET将期望单个对象,但得到一个列表。错误消息基本上就是告诉你这一点。
试试看:
List<RootObject> values = JsonConvert.DeserializeObject<List<RootObject>>(receiving);
enter code here
///类
公共类MambuData{public mambuData[]mambuDataSet{get;set;}}
public class mambuData
{
public string encodedKey { get; set; }
public string id { get; set; }
public DateTime creationDate { get; set; }
public DateTime lastModifiedDate { get; set; }
public string name { get; set; }
public string phoneNumber { get; set; }
public string emailAddress { get; set; }
}
///方法
私有字符串ParseMambuData(字符串streamResult){string R=";
List<mambuData> mambuDataList = JsonConvert.DeserializeObject<List<mambuData>>(streamResult);
foreach (mambuData m in mambuDataList)
{
//put data in db if you want
}
return R;
}