【问题标题】:Cannot deserialize the current JSON object..into type 'System.Collections.Generic.List`1[System.String]'无法将当前 JSON 对象反序列化..转换为“System.Collections.Generic.List`1[System.String]”类型
【发布时间】:2014-07-28 16:23:03
【问题描述】:

完全错误

无法反序列化当前 JSON 对象(例如 {"name":"value"}) 进入类型 'System.Collections.Generic.List`1[System.String]' 因为 该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化 正确。要修复此错误,请将 JSON 更改为 JSON 数组 (例如 [1,2,3])或更改反序列化类型,使其成为正常的 .NET 类型(例如,不是像整数这样的原始类型,也不是集合 可以从 JSON 反序列化的数组或列表等类型 目的。 JsonObjectAttribute 也可以添加到类型中来强制它 从 JSON 对象反序列化。小路 'CEC1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4',第 2 行,位置 42。

在这段代码之前,有一个运行良好...

var highlighting = JsonConvert.DeserializeObject<Object>(jsonDeserialized["highlighting"].ToString());

这为我们提供了一些来自搜索引擎的数据:

{\r\n  "C6C1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4": {\r\n    "Title_fr":
[\r\n      "<em>Chris</em> <em>Cep</em>"\r\n    ]\r\n  } ,\r\n 
"874BE5B1-FAA0-463F-98DD-8532D4D80178": {\r\n    "Content_fr": [\r\n  
" <em>Chris</em> <em>Cep</em>, Ctifl \n\n9 Info - Tgif /
January - February 2001\n\nThe Tgif on the Net\nInquire"\r\n   
]\r\n  } ,\r\n  "44FA5B99-9472-499C-9827-646A511068DA": {\r\n   
"Content_fr": [\r\n      " S\n\nHorti\nThe
database\ndocument\n<em>Chris</em> <em>Cep</em>, Iso
Rever,\nJeane Samuel"\r\n    ]\r\n  } \r\n}

//formatted - http://jsonblob.com/53d24a78e4b0ed12c5a6b1df

{
  "C6C1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4": {
    "Title_fr": [
      "<em>Chris</em> <em>Cep</em>"
    ]
  },
  "874BE5B1-FAA0-463F-98DD-8532D4D80178": {
    "Content_fr": [
      " <em>Chris</em> <em>Cep</em>, Ctifl \n\n9 Info - Tgif /    January - February 2001\n\nThe Tgif on the Net\nInquire"
    ]
  },
  "44FA5B99-9472-499C-9827-646A511068DA": {
    "Content_fr": [
      " S\n\nHorti\nThe database\ndocument\n<em>Chris</em> <em>Cep</em>, Iso Rever,\nJeane Samuel"
    ]
  }
}

我认为,问题在于以下三个对象的“命名”都不同。 IE。我不能这样做JsonConvert.DeserializeObject&lt;List&lt;string&gt;&gt;(jsonDeserialized["highlighting"]["CAN'T-REFERENCE-ANYTHING-HERE"].ToString()),因为这些节点的名称是像C6C1B0C9-A87A-4A9E-A017-AEFC3CEDCBD4 这样的GUID。

我仍然对 JSON 比较陌生 - 我在这里缺少什么?如何将“突出显示”中的三个对象放入某种 List 中?

我的解决方案:

    private class Highlighters //defined elsewhere
    {
        public string IdGlobal;
        public string FieldName;
        public string FieldValue;
    }

    var highlighters = new List<Highlighters>();
    var allHighlights = JsonConvert.DeserializeObject<IDictionary<string, object>>(jsonDeserialized["highlighting"].ToString());
    foreach (var entry in allHighlights)
    {
        string guid = entry.Key;
        string j = entry.Value.ToString();
        var insideEntry = JsonConvert.DeserializeObject<IDictionary<string, object>>(j);

        foreach (var h in insideEntry)
        {
            string highlightedField = h.Key;
            string highlightedValue = h.Value.ToString().Substring(1, h.Value.ToString().LastIndexOf("]") - 1).Trim().Trim('"').Trim();//a little cleaning

            highlighters.Add(new Highlighters{IdGlobal=guid, FieldName = highlightedField, FieldValue = highlightedValue});
        }
    }

【问题讨论】:

    标签: c# json


    【解决方案1】:

    如果您对键(在您的示例中是 GUID)不感兴趣,请将 JSON 反序列化为 Dictionary&lt;string, JObject&gt; 并访问结果字典的 .Values

    【讨论】:

    • 谢谢,这就是我最终做的事情(但我也确实需要 GUID)- 在上面编辑。
    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多