简体   繁体   English

如何在Windows Store应用程序C#中将JSON解析为数组列表

[英]how to parse json to array list in windows store app c#

i am trying to parse json to array in windows store app using c#. 我正在尝试使用C#将json解析为Windows应用商店中的数组。

my json is as follows 我的json如下

{
"catlist": [
    {
        "area_id": "1",
        "area_name": "Ko",
        "categories": [
            {
                "cat_id": "1",
                "cat_name": "Hot",
                "chk_value": "2",
                "sublist": [
                    {
                        "sub_id": "5",
                        "sub_name": "Beach"
                    },
                    {
                        "sub_id": "6",
                        "sub_name": "Beach"
                    },
                    {
                        "sub_id": "7",
                        "sub_name": "Beach"
                    }
                ]
            },
            {
                "cat_id": "2",
                "cat_name": "Dining",
                "chk_value": "1",
                "sublist": [
                    {
                        "sub_id": "1",
                        "sub_name": "Road"
                    },
                    {
                        "sub_id": "2",
                        "sub_name": "Soi "
                    }
                ]
            }]}

what is the easiest way to parse it into an array of json elements and use it for data binding 将其解析为json元素数组并将其用于数据绑定的最简单方法是什么

Using Json.Net 使用Json.Net

var array = Newtonsoft.Json.Linq.JArray.Parse(jsonString);

Using WinRT API's JsonArray class 使用WinRT API的JsonArray

var array = Windows.Data.Json.JsonArray.Parse(jsonString);

BTW, your JSON is invalid according to JSON Lint 顺便说一句,根据JSON Lint ,您的JSON无效


According to me this is valid JSON array 根据我的说法,这是有效的JSON数组

[
    {
        "catlist": [
            {
                "area_id": "1",
                "area_name": "Ko",
                "categories": [
                    {
                        "cat_id": "1",
                        "cat_name": "Hot",
                        "chk_value": "2",
                        "sublist": [
                            {
                                "sub_id": "5",
                                "sub_name": "Beach"
                            },
                            {
                                "sub_id": "6",
                                "sub_name": "Beach"
                            },
                            {
                                "sub_id": "7",
                                "sub_name": "Beach"
                            }
                        ]
                    },
                    {
                        "cat_id": "2",
                        "cat_name": "Dining",
                        "chk_value": "1",
                        "sublist": [
                            {
                                "sub_id": "1",
                                "sub_name": "Road"
                            },
                            {
                                "sub_id": "2",
                                "sub_name": "Soi "
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

If you want to deserialize the JSON, you should like this. 如果要反序列化JSON,则应该这样。

var Obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject[]>(jsonString);

public class Sublist
{
    public string sub_id { get; set; }
    public string sub_name { get; set; }
}

public class Category
{
    public string cat_id { get; set; }
    public string cat_name { get; set; }
    public string chk_value { get; set; }
    public List<Sublist> sublist { get; set; }
}

public class Catlist
{
    public string area_id { get; set; }
    public string area_name { get; set; }
    public List<Category> categories { get; set; }
}

public class RootObject
{
    public List<Catlist> catlist { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM