C#。如何反序化这个Json

时间:2017-06-21 12:05:36

标签: c# json deserialization

这是我的第一篇帖子:) 我有反序列化json字符串的问题。 这是一个例子:

{
"packs": {
    "category1": {
        "Element1": {
            "url": "Url1",
            "name": "File 1"
        },
        "Element2": {
            "url": "Url2",
            "name": "File 2"
        },
        "Element3": {
            "url": "Url3",
            "name": "File 3"
        },
        "Element4": {
            "url": "Url4",
            "name": "File 4"
        },
        "Element5": {
            "url": "Url5",
            "name": "File 5"
        },
        "Element6": {
            "url": "Url6",
            "name": "File 6"
        },
        "Element7": {
            "url": "Url7",
            "name": "File 7"
        },
        "Element8": {
            "url": "Url8",
            "name": "File 8"
        },
        "Element9": {
            "url": "Url9",
            "name": "File 9"
        },
        "Element10": {
            "url": "Url10",
            "name": "File 10"
        }
    },
    "category2": {
        "short": {
            "url": "Url1",
            "name": "Short "
        },
        "medium": {
            "url": "Url2",
            "name": "Medium "
        },
        "long": {
            "url": "Url3",
            "name": "Long "
        }
    }
}
}

这是代码反序列化:

var json = client.GetStringAsync(string.Format(Url));

var jsonDeserialize = JsonConvert.DeserializeObject<Models.PacksModel>(json.Result);

这是模特:

    public class PacksModel
{
    public Cathegory packs { get; set; }
}


public class Cathegory
{
    public JContainer category1 { get; set; }
    public JContainer category2 { get; set; }
}

我不知道从Element获取数据。什么sugest? 谢谢回答!

4 个答案:

答案 0 :(得分:1)

由于类别和元素名称往往会有所不同,因此最好将它们定义为词典:

public class PacksModel
{
    public Dictionary<string, Dictionary<string, Item>> packs { get; set; }
}

public class Item
{
    public string url { get; set; }
    public string name { get; set; }
}

// ......................................

var obj = JsonConvert.DeserializeObject<PacksModel>(json);
Console.WriteLine(obj.packs["category2"]["medium"].url);

演示:https://dotnetfiddle.net/h4BYOE

答案 1 :(得分:0)

尝试使用此对象反序列化json:

namespace ConsoleApp3.Domain
{
    public class Element1
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element2
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element3
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element4
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element5
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element6
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element7
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element8
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element9
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Element10
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Category1
    {
        public Element1 Element1 { get; set; }
        public Element2 Element2 { get; set; }
        public Element3 Element3 { get; set; }
        public Element4 Element4 { get; set; }
        public Element5 Element5 { get; set; }
        public Element6 Element6 { get; set; }
        public Element7 Element7 { get; set; }
        public Element8 Element8 { get; set; }
        public Element9 Element9 { get; set; }
        public Element10 Element10 { get; set; }
    }

    public class Short
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Medium
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Long
    {
        public string url { get; set; }
        public string name { get; set; }
    }

    public class Category2
    {
        public Short @short { get; set; }
        public Medium medium { get; set; }
        public Long @long { get; set; }
    }

    public class Packs
    {
        public Category1 category1 { get; set; }
        public Category2 category2 { get; set; }
    }

    public class RootObject
    {
        public Packs packs { get; set; }
    }
}

并将其反序列化:

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var jsonfile = File.ReadAllText("jsonfile.json");
            var deserializedFile = JsonConvert.DeserializeObject<Domain.RootObject>(jsonfile);
            // Do something with your object
        }
    }
}

答案 2 :(得分:0)

使用您的JSON字符串,您的模型应如下所示:

public class Element1
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element2
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element3
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element4
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element5
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element6
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element7
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element8
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element9
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Element10
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Category1
{
    public Element1 Element1 { get; set; }
    public Element2 Element2 { get; set; }
    public Element3 Element3 { get; set; }
    public Element4 Element4 { get; set; }
    public Element5 Element5 { get; set; }
    public Element6 Element6 { get; set; }
    public Element7 Element7 { get; set; }
    public Element8 Element8 { get; set; }
    public Element9 Element9 { get; set; }
    public Element10 Element10 { get; set; }
}

public class Short
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Medium
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Long
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Category2
{
    public Short @short { get; set; }
    public Medium medium { get; set; }
    public Long @long { get; set; }
}

public class Packs
{
    public Category1 category1 { get; set; }
    public Category2 category2 { get; set; }
}

public class PacksModel
{
    public Packs packs { get; set; }
}

然后:

var json = client.GetStringAsync(string.Format(Url));
var jsonDeserialize = JsonConvert.DeserializeObject<Models.PacksModel>(json.Result);

注意:如果您可以更改JSON字符串结构,则应使用Arrays for Elements。

例如,如果您将JSON字符串结构更改为:

{
    "packs": {
        "category1": {

            "Elements": [{
                    "url": "Url1",
                    "name": "File 1"
                },
                {
                    "url": "Url2",
                    "name": "File 2"
                },
                {
                    "url": "Url3",
                    "name": "File 3"
                },
                {
                    "url": "Url4",
                    "name": "File 4"
                },
                {
                    "url": "Url5",
                    "name": "File 5"
                },
                {
                    "url": "Url6",
                    "name": "File 6"
                },
                {
                    "url": "Url7",
                    "name": "File 7"
                },
                {
                    "url": "Url8",
                    "name": "File 8"
                },
                {
                    "url": "Url9",
                    "name": "File 9"
                },
                {
                    "url": "Url10",
                    "name": "File 10"
                }
            ]
        },
        "category2": {
            "short": {
                "url": "Url1",
                "name": "Short "
            },
            "medium": {
                "url": "Url2",
                "name": "Medium "
            },
            "long": {
                "url": "Url3",
                "name": "Long "
            }
        }
    }
}

您可以使用此模型:

public class Element
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Category1
{
    public List<Element> Elements { get; set; }
}

public class Short
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Medium
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Long
{
    public string url { get; set; }
    public string name { get; set; }
}

public class Category2
{
    public Short @short { get; set; }
    public Medium medium { get; set; }
    public Long @long { get; set; }
}

public class Packs
{
    public Category1 category1 { get; set; }
    public Category2 category2 { get; set; }
}

public class PacksModel
{
    public Packs packs { get; set; }
}

然后:

var json = client.GetStringAsync(string.Format(Url));
var jsonDeserialize = JsonConvert.DeserializeObject<Models.PacksModel>(json.Result);

使用此JSON字符串结构的努力是,您可以使用灵活的元素数量!

答案 3 :(得分:0)

谢谢大家的回答。 我正在学习Xamarin.Forms中的编程,而这不是我的json。

HttpClient client = new HttpClient();
var json = client.GetStringAsync(string.Format(Url));
dynamic deserializeJson = JsonConvert.DeserializeObject<dynamic>(json.Result);



 private void writeDataOnModelMusicUrl()
    {
        foreach (var part in deserializeJson["packs"]["category1"])
        {

            foreach (var elements in part)
            {
                var url = elements["url"];
                var name = elements["name"];
                //I'm doing something
            }
        }

        foreach (var part in deserializeJson["packs"]["category2"])
        {

            foreach (var elements in part)
            {
                var url = elements["url"];
                var name = elements["name"];
                //I'm doing something
            }
        }
    }

我认为这就够了:)

可以关闭主题。