How to Deserializing JSON Object Array with Xamarin forms

↘锁芯ラ 提交于 2021-01-29 14:48:33

问题


I am trying to convert this json array to show data in the list view. But i getting this error when i parsing this.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Apps.Models.RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'id', line 1, position 6.

JSON Files look like:

{
    "id": 2,
    "parent_id": 1,
    "name": "Default Category",
    "is_active": true,
    "position": 1,
    "level": 1,
    "product_count": 4503,
    "children_data": [
        {
            "id": 848,
            "parent_id": 2,
            "name": "GROCERIES",
            "is_active": true,
            "position": 0,
            "level": 2,
            "product_count": 198,
            "children_data": [
                {
                    "id": 849,
                    "parent_id": 848,
                    "name": "SUGAR",
                    "is_active": true,
                    "position": 0,
                    "level": 3,
                    "product_count": 13,
                    "children_data": [
                        {
                            "id": 850,
                            "parent_id": 849,
                            "name": "RING",
                            "is_active": true,
                            "position": 0,
                            "level": 4,
                            "product_count": 3,
                            "children_data": []
                        }
                    ]
                },
                {
                    "id": 851,
                    "parent_id": 848,
                    "name": "RICE",
                    "is_active": true,
                    "position": 0,
                    "level": 3,
                    "product_count": 47,
                    "children_data": [
                        {
                            "id": 852,
                            "parent_id": 851,
                            "name": "RING",
                            "is_active": true,
                            "position": 0,
                            "level": 4,
                            "product_count": 1,
                            "children_data": []
                        }
                    ]
                }
            ]
        },
    {
        "id": 2017,
        "parent_id": 2,
        "name": "Food Basket",
        "is_active": true,
        "position": 2,
        "level": 2,
        "product_count": 19,
        "children_data": []
    }
]}

My Model Class is: I create a model class like this.

public class ChildrenData2
{
     public string id { get; set; }
     public string parent_id { get; set; }
     public string name { get; set; }
     public string is_active { get; set; }
     public string position { get; set; }
     public string level { get; set; }
     public string product_count { get; set; }
     public List children_data { get; set; }
}

public class ChildrenData
{
    public ChildrenData2 children_data { get; set; }
}

public class RootObject
{
    public List<ChildrenData> children_data { get; set; }
}

JSON Deserialize Code: I am trying this code to parse the json array.

var client = new System.Net.Http.HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
                    Constants.AccessName, Constants.AccessToken);

var json = await client.GetAsync(Constants.BaseApiAddress + "V1/categories");
string contactsJson = await json.Content.ReadAsStringAsync();

if (contactsJson != "")
{
       //Converting JSON Array Objects into generic list  
       Newtempdata = JsonConvert.DeserializeObject<List<RootObject>>(contactsJson);
}

回答1:


For the given JSON your model will look like this

public class ChildrenData
{
    public int id { get; set; }
    public int parent_id { get; set; }
    public string name { get; set; }
    public bool is_active { get; set; }
    public int position { get; set; }
    public int level { get; set; }
    public int product_count { get; set; }
    public List<ChildrenData> children_data { get; set; }
}

Then you can use JsonConvert to convert the JSON to Object. Also remember it is not List<ChildrenData>

var test = JsonConvert.DeserializeObject<ChildrenData>(json);



回答2:


Use below site for creating Model Class

http://json2csharp.com/

and then use JsonDeserialize to convert response in model

var result= JsonConvert.DeserializeObject<typeOfModel>(json);



来源:https://stackoverflow.com/questions/60165218/how-to-deserializing-json-object-array-with-xamarin-forms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!