3

我正在尝试反序列化 Json 字符串 usign Json.NET。我对它有一个基本的了解,但我已经尝试了好几天但无法让它工作,这是我的代码。(请忽略任何不必要/额外的代码,请记住,我正在根据研究后发现的内容尝试多种解决方案)。

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 Garc\u00eda",
            "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 数组反序列化。JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。

路径'',第 1 行,位置 1。

正如我之前所说,我对 Json 格式有基本的了解,并且我一直在尝试使用不同类型的类来反序列化并获得相同的结果,任何提示或任何关于问题可能是什么的想法?除了 json.NET,我还坚持使用 .NET 2.0 而不是 3rd 方库。

感谢您的关注。

  • 编辑 *

显然,即使我尝试声明列表值,变量“值”也不会在实际上下文中创建,任何解决方法。

4

2 回答 2

0

您的 JSON 是一个包含一个对象的数组。

这意味着 JSON.NET 将期待单个对象但得到一个列表。错误消息基本上是在告诉你。

试试这个:

List<RootObject> values = JsonConvert.DeserializeObject<List<RootObject>>(receiving);
于 2012-08-31T16:01:19.433 回答
-2

enter code here/// 类

公共类 MambuData { 公共 mambuData [] mambuDataS { 获取;放; } }

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(string 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;

    }
于 2015-03-30T17:59:50.000 回答