【问题标题】:Deserializing JSON using Newtonsoft in C#在 C# 中使用 Newtonsoft 反序列化 JSON
【发布时间】:2015-10-05 15:38:30
【问题描述】:

我有以下 JSON:

[
    {
        "name": "codeURL",
        "value": "abcd"
    },
    {
        "name": "authURL",
        "value": "fghi"
    }
]

我创建了以下对象:

public class ConfigUrlModel {
    [JsonProperty("name")]
    public abstract string name { get; set; }
    [JsonProperty("value")]
    public abstract string value { get; set; }
}

public class ConfigUrlsModel {
    [JsonProperty]
    public List<ConfigUrlModel> ConfigUrls { get; set; }
}

我正在反序列化以下行:

resultObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigUrlsModel>(resultString);
ConfigUrlsModel result = resultObject as ConfigUrlsModel;

我收到以下错误:

Exception JsonSerializationException with no inner exception: Cannot deserialize JSON array into type 'Microsoft.Xbox.Samples.Video.Services.Jtv.Models.ConfigUrl.ConfigUrlsModel'.
Exception JsonSerializationException with no inner exception: Cannot deserialize JSON array into type 'Microsoft.Xbox.Samples.Video.Services.Jtv.Models.ConfigUrl.ConfigUrlsModel'.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(Type objectType, JsonContract contract)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contr   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(Type objectType, JsonContract contract)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contrNavigationService:OnNavigateToMessage PageSourceUri=/Microsoft.Xbox.Sample.UI;component/ErrorPrompt/ErrorPromptView.xaml

我做错了什么?我该如何解决这个问题?

【问题讨论】:

  • JtvConfigUrlsModel 在哪里??
  • @PranavPatel Jtv 是个错误。它是 ConfigUrlsModel。

标签: c# json json.net


【解决方案1】:

JSON容器是一个数组,而不是一个对象,因此反序列化它:

var configUrls = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ConfigUrlModel>>(resultString);
var result = new ConfigUrlsModel { ConfigUrls = configUrls }; // If you still need the root object.

JSON 数组是值[value1, value2, ..., value] 的有序列表,这就是您的问题中显示的内容。 Json.NET will convert .NET arrays and collections to JSON arrays,所以需要反序列化为集合类型。

【讨论】:

    【解决方案2】:

    您发送的 JSON 是一个数组,但您正试图将其反序列化为一个对象。以太更改您的 JSON,使其与顶层的对象定义匹配,并具有匹配属性,如下所示:

    {
       "ConfigUrls":[
          {
             "name":"codeURL",
             "value":"abcd"
          },
          {
             "name":"authURL",
             "value":"fghi"
          }
       ]
    }
    

    或将您的反序列化调用更改为:

    var urls = DeserializeObject<List<ConfigUrlModel>>(json);
    

    这将返回一个List&lt;ConfigUrlModel&gt;,您可以直接使用它,也可以根据需要将其包装在ConfigUrlModels 实例中。

    此外,可以通过创建custom newtonsoft JsonConverter 子类直接将此 JSON 反序列化为所需的类。但这会使代码不太清晰,因此请尽可能避免使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2017-05-26
      • 2021-10-13
      相关资源
      最近更新 更多