无法将JSON数组反序列化为类型-JSON.NET

本文关键字:类型 -JSON NET 反序列化 数组 JSON | 更新日期: 2023-09-27 18:25:45

我试图将json数据反序列化为模型类,但失败了。我是这样做的:

    public CountryModel GetCountries() {
        using (WebClient client = new WebClient()) {
            var result = client.DownloadString("http://api.worldbank.org/incomeLevels/LIC/countries?format=json");
            var output = JsonConvert.DeserializeObject<List<CountryModel>>(result);
            return output.First();
        }
    }

我的模型是这样的:

public class CountryModel
{
    public int Page { get; set; }
    public int Pages { get; set; }
    public int Per_Page { get; set; }
    public int Total { get; set; }
    public List<Country> Countries { get; set; }
}
public class Country
{
    public int Id { get; set; }
    public string Iso2Code { get; set; }
    public string Name { get; set; }
    public Region Region { get; set; }
}
public class Region
{
    public int Id { get; set; }
    public string Value { get; set; }
}

你可以看到我在这里得到的Json:http://api.worldbank.org/incomeLevels/LIC/countries?format=json

这是我得到的错误:

无法将JSON数组反序列化为类型"Mvc4AsyncSample.Models.CountryModel"。第1行,位置1。

无法将JSON数组反序列化为类型-JSON.NET

您必须编写一个自定义JsonConverter:

    public class CountryModelConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            if (objectType == typeof(CountryModel))
            {
                return true;
            }
            return false;
        }
        public override object ReadJson(JsonReader reader, Type objectType
            , object existingValue, JsonSerializer serializer)
        {
            reader.Read(); //start array
            //reader.Read(); //start object
            JObject obj = (JObject)serializer.Deserialize(reader);
            //{"page":1,"pages":1,"per_page":"50","total":35}
            var model = new CountryModel();
            model.Page = Convert.ToInt32(((JValue)obj["page"]).Value);
            model.Pages = Convert.ToInt32(((JValue)obj["pages"]).Value);
            model.Per_Page = Int32.Parse((string) ((JValue)obj["per_page"]).Value);
            model.Total = Convert.ToInt32(((JValue)obj["total"]).Value);
            reader.Read(); //end object
            model.Countries = serializer.Deserialize<List<Country>>(reader);
            reader.Read(); //end array
            return model;
        }
        public override void WriteJson(JsonWriter writer, object value
            , JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

并用那个转换器标记CountryModel(我还必须将一些int切换到string):

    [JsonConverter(typeof(CountryModelConverter))]
    public class CountryModel
    {
        public int Page { get; set; }
        public int Pages { get; set; }
        public int Per_Page { get; set; }
        public int Total { get; set; }
        public List<Country> Countries { get; set; }
    }
    public class Country
    {
        public string Id { get; set; }
        public string Iso2Code { get; set; }
        public string Name { get; set; }
        public Region Region { get; set; }
    }
    public class Region
    {
        public string Id { get; set; }
        public string Value { get; set; }
    }

然后你应该能够像这样反序列化:

var output = JsonConvert.DeserializeObject<CountryModel>(result);

这看起来像是在JSON中表示XML的尝试(不是很好)。JSON如下所示:

[
  {
    "page": 1,
    …
  },
  [
    {
      "id": "AFG",
      "name": "Afghanistan",
      …
    },
    {
      "id": "BDI",
      "name": "Burundi",
      …
    },
    …
  ]
]

虽然一个合理的JSON(顺便说一下,它会很好地映射到您的模型)看起来是这样的:

{
  "page": 1,
  …,
  "countries": [
    {
      "id": "AFG",
      "name": "Afghanistan",
      …
    },
    {
      "id": "BDI",
      "name": "Burundi",
      …
    },
    …
  ]
}

如果您确定要使用JSON(而不是XML),可以先将JSON反序列化为JSON.NET的对象模型,然后将其反序列化为您的模型:

var json = client.DownloadString("http://api.worldbank.org/incomeLevels/LIC/countries?format=json");
var array = (JArray)JsonConvert.DeserializeObject(json);
var serializer = new JsonSerializer();
var countryModel = serializer.Deserialize<CountryModel>(array[0].CreateReader());
countryModel.Countries = serializer.Deserialize<List<Country>>(array[1].CreateReader());
return countryModel;

不要忘记将Id属性更改为string,因为它们就是这样。

您的模型与JSON结构不匹配。看起来你错过了最后6处房产。

{
"id": "AFG",
"iso2Code": "AF",
"name": "Afghanistan",
"region": {
    "id": "SAS",
    "value": "South Asia"
},
"adminregion": {
    "id": "SAS",
    "value": "South Asia"
},
"incomeLevel": {
    "id": "LIC",
    "value": "Low income"
},
"lendingType": {
    "id": "IDX",
    "value": "IDA"
},
"capitalCity": "Kabul",
"longitude": "69.1761",
"latitude": "34.5228"

}