反序列化不同对象类型的数组

Deserializing an array of different object types

使用 Json.NET 库,我在反序列化一些作为数组返回的 json 时遇到了问题。 json 是一个数组,包含一些分页信息作为对象和国家/地区对象数组。这是返回的示例 json:

[
    {
        "page": 1,
        "pages": 6,
        "per_page": "50",
        "total": 262
    },
    [
        {
            "id": "ABW",
            "iso2Code": "AW",
            "name": "Aruba",
            "region": {
                "id": "LCN",
                "value": "Latin America & Caribbean (all income levels)"
            },
            "adminregion": {
                "id": "",
                "value": ""
            },
            "incomeLevel": {
                "id": "NOC",
                "value": "High income: nonOECD"
            },
            "lendingType": {
                "id": "LNX",
                "value": "Not classified"
            },
            "capitalCity": "Oranjestad",
            "longitude": "-70.0167",
            "latitude": "12.5167"
        }
    ]
]

我正在尝试反序列化为以下类型:

class CountriesQueryResults
{
    public PagingInfo PageInfo { get; set; }
    public List<CountryInfo> Countries { get; set; }
}

class PagingInfo
{
    public int Page { get; set; }
    public int Pages { get; set; }
    [JsonProperty("per_page")]  
    public int ResultsPerPage { get; set; }
    public int Total { get; set; }
}

class CountryInfo
{
    public string Id { get; set; }
    public string Iso2Code { get; set; }
    public string Name { get; set; }
    public string Longitude { get; set; }
    public string Latitude { get; set; }
    public string CapitalCity { get; set; }
    public CompactIdentifier Region { get; set; }
    public CompactIdentifier AdminRegion { get; set; }
    public CompactIdentifier IncomeLevel { get; set; }
    public CompactIdentifier LendingType { get; set; }  
}

class CompactIdentifier
{
    public string Id { get; set; }
    public string Value { get; set; }
}

我这样调用 DeserializeObject:

var data = JsonConvert.DeserializeObject<List<CountriesQueryResults>>(response);

我收到以下错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'CountriesQueryResults' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

我一直试图从 the documentation 那里得到答案,但我似乎无法弄清楚。任何帮助将不胜感激。

因为你的json是这样的[ {....} , [{....}] ],你只能将它反序列化为一个对象数组(第一项是一个对象,第二项是另一个数组)。

我认为将其转换为 C# 对象的最简单方法是:

var jArr = JArray.Parse(jsonstr);
var pageInfo = jArr[0].ToObject<PagingInfo>();
var countryInfos = jArr[1].ToObject<List<CountryInfo>>();

Class 定义为:

public class PagingInfo
{
    public int page { get; set; }
    public int pages { get; set; }
    [JsonProperty("per_page")]
    public int ResultsPerPage { get; set; }
    public int total { get; set; }
}

public class Region
{
    public string id { get; set; }
    public string value { get; set; }
}

public class AdminRegion
{
    public string id { get; set; }
    public string value { get; set; }
}

public class IncomeLevel
{
    public string id { get; set; }
    public string value { get; set; }
}

public class LendingType
{
    public string id { get; set; }
    public string value { get; set; }
}

public class CountryInfo
{
    public string id { get; set; }
    public string iso2Code { get; set; }
    public string name { get; set; }
    public Region region { get; set; }
    public AdminRegion adminregion { get; set; }
    public IncomeLevel incomeLevel { get; set; }
    public LendingType lendingType { get; set; }
    public string capitalCity { get; set; }
    public string longitude { get; set; }
    public string latitude { get; set; }
}

PS:您可以根据需要将 属性 名称的第一个字符更改为大写。我使用 http://json2csharp.com/ 自动生成那些 类.