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

时间:2015-02-14 20:28:57

标签: c# json.net

使用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得到答案,但我似乎无法弄明白。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:6)

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

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

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

类定义将是:

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/自动生成这些类。