如何使用 Xamarin 表单反序列化 JSON 对象数组 [英] How to Deserializing JSON Object Array with Xamarin forms

查看:36
本文介绍了如何使用 Xamarin 表单反序列化 JSON 对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换此 json 数组以在列表视图中显示数据.但是我在解析这个时收到这个错误.

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

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型System.Collections.Generic.List`1[Apps.Models.RootObject]",因为该类型需要一个 JSON 数组(例如[1,2,3]) 正确反序列化.要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型)可以从 JSON 对象反序列化的数组或列表).JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化.路径 'id',第 1 行,位置 6.

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 文件如下所示:

{
    "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 反序列化代码:我正在尝试使用此代码来解析 json 数组.

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);
}

推荐答案

使用下面的站点创建模型类

Use below site for creating Model Class

http://json2csharp.com/

然后使用 JsonDeserialize 转换模型中的响应

and then use JsonDeserialize to convert response in model

var result= JsonConvert.DeserializeObject(json);

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

这篇关于如何使用 Xamarin 表单反序列化 JSON 对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆