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

时间:2012-02-26 12:06:27

标签: c# .net json serialization json.net

我正在尝试将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位。

3 个答案:

答案 0 :(得分:17)

您必须编写自定义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);

答案 1 :(得分:12)

这看起来像是一种(不是很好)尝试用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,因为它们就是这样。

答案 2 :(得分:-1)

您的模型与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"

}