C# 如何使用Xamarin表单反序列化JSON对象数组

C# 如何使用Xamarin表单反序列化JSON对象数组,c#,xamarin.forms,C#,Xamarin.forms,我正在尝试将这个json数组转换为在列表视图中显示数据。但我在分析这个时发现了这个错误 无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.List`1[Apps.Models.RootObject]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是int

我正在尝试将这个json数组转换为在列表视图中显示数据。但我在分析这个时发现了这个错误

无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.List`1[Apps.Models.RootObject]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。 路径“id”,第1行,位置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": []
    }
]}
我的模型类是: 我创建了一个这样的模型类

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; }
}
公共类childrendata 2
{
公共字符串id{get;set;}
公共字符串父\u id{get;set;}
公共字符串名称{get;set;}
公共字符串是_active{get;set;}
公共字符串位置{get;set;}
公共字符串级别{get;set;}
公共字符串乘积_计数{get;set;}
公共列表子项\u数据{get;set;}
}
公共类儿童数据
{
public ChildrenData2 children_数据{get;set;}
}
公共类根对象
{
公共列表子项\u数据{get;set;}
}
JSON反序列化代码: 我尝试用这段代码来解析json数组

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);
}
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();
如果(contactsJson!=“”)
{
//将JSON数组对象转换为通用列表
Newtempdata=JsonConvert.DeserializeObject(contactsJson);
}

对于给定的JSON,您的模型将如下所示

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

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

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


var result=
JsonConvert.DeserializeObject(json)

您可以复制JSON并在VS中使用
Edit=>Paste Special=>Paste JSON as class
var test = JsonConvert.DeserializeObject<ChildrenData>(json);