簡體   English   中英

反序列化不同對象類型的數組

[英]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 (eg {"name":"value"}) into type 'CountriesQueryResults' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.

我一直試圖從文檔中得到答案,但我似乎無法弄明白。 任何幫助,將不勝感激。

由於你的json就像這個[ {....} , [{....}] ] ,你只能將它反序列化為一個對象數組(其中第一項是一個對象,第二項是另一個數組)。

我認為將其轉換為ac#對象的最簡單方法是:

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/自動生成這些類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM