如何将多级json数据转换为C#Object? [英] how to conver multi level json data to C# Object?

查看:249
本文介绍了如何将多级json数据转换为C#Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道json有几个问题,但没有和我一样。我的问题

如何将这个jsonString转换为c#对象。我试了好几次。这个意外的问题让我感到沮丧。我需要知道我的问题,我应该怎么做....



I know there are several problem with json but nothing as same as to me. My problem
How to convert this jsonString to c# object. I've tried several time. This unexpected problem made me frustrated. I need to know whats problem with me and what should i do....

{ "status": 200, "status_messages": "Success ", "data": [ { "Alarm And Alert": [ { "0": "1", "1": "Alarm And Alert 1", "2": "0", "id": "1", "name": "Alarm And Alert 1", "islock": "0" }, { "0": "6", "1": "Alarm And Alert 6", "2": "0", "id": "6", "name": "Alarm And Alert 6", "islock": "0" } ] }, { "Animal": [ { "0": "7", "1": "Bird", "2": "0", "id": "7", "name": "Bird", "islock": "0" }, { "0": "13", "1": "Funny Animals", "2": "0", "id": "13", "name": "Funny Animals", "islock": "0" } ] }, { "Dj": [ { "0": "14", "1": "Dj 1", "2": "0", "id": "14", "name": "Dj 1", "islock": "0" }, { "0": "15", "1": "Dj 2", "2": "0", "id": "15", "name": "Dj 2", "islock": "0" }, { "0": "18", "1": "Dj 5", "2": "0", "id": "18", "name": "Dj 5", "islock": "0" } ] }, { "Rap": [ { "0": "71", "1": "Rap Ringtones 1", "2": "0", "id": "71", "name": "Rap Ringtones 1", "islock": "0" }, { "0": "77", "1": "Rap Ringtones 7", "2": "0", "id": "77", "name": "Rap Ringtones 7", "islock": "0" } ] } ] }





我的班级:





My Class:

public  class Category
    {
        public string __invalid_name__0 { get; set; }
        public string id { get; set; }
        public string __invalid_name__1 { get; set; }
        public string name { get; set; }
        public string __invalid_name__2 { get; set; }
        public string islock { get; set; }
    }

public  class CategoryCollection
    {
      public List<category> CategoryRingtone { get; set; }
    }

public  class RootObject
    {
        public int status { get; set; }
        public string status_messages { get; set; }
        public List<categorycollection> data { get; set; }
    }

categoriesJsonString = await Downloader.LoadCategoriesFromServer();
            RootObject rootObject = new RootObject();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(categoriesJsonString));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(rootObject.GetType());
            rootObject = ser.ReadObject(ms) as RootObject;
            ms.Close();

推荐答案

你好,



有很多可以帮助你做这种事情的工具。



首先来看看 http ://jsonformatter.curiousconcept.com/ [ ^ ]这将帮助您了解json字符串的组织。



还可以查看 http:/ /json2csharp.com/ [ ^ ]。这将生成与你的Json相匹配的C#类



所以在你的情况下这就是你得到的:





Hello,

There are plenty of tools that can help you doing this kind of stuff.

First have a look at http://jsonformatter.curiousconcept.com/[^] this will help you understand the organisation of your json string.

And also have a look at http://json2csharp.com/[^]. This will generate the C# classes that match your Json

So in you case this is what you would get:


public class AlarmAndAlert
{
    public string __invalid_name__0 { get; set; }
    public string id { get; set; }
    public string __invalid_name__1 { get; set; }
    public string name { get; set; }
    public string __invalid_name__2 { get; set; }
    public string islock { get; set; }
}

public class Animal
{
    public string __invalid_name__0 { get; set; }
    public string id { get; set; }
    public string __invalid_name__1 { get; set; }
    public string name { get; set; }
    public string __invalid_name__2 { get; set; }
    public string islock { get; set; }
}

public class Datum
{
    public List<AlarmAndAlert> __invalid_name__Alarm And Alert { get; set; }
    public List<Animal> Animal { get; set; }
}

public class RootObject
{
    public int status { get; set; }
    public string status_messages { get; set; }
    public List<Datum> data { get; set; }
}







Valery。




Valery.


DataContractJSONSerializer 实际上是一个非常好的选项,但如果你首先使用它进行序列化,它就完全有效;然后它将为您提供一个Javascript / JSON示例,您可以进一步使用它来为.NET端的反序列化输入。



如果数据总是来自Javascript,或者已经提供了数据,那么可能不是很方便。



另一种特别适合Web开发的方法是使用类 System.Web.Script.Serialization.JavaScriptSerializer

http://msdn.microsoft.com/en -us / library / system.web.script.serialization.javascriptserializer%28v = vs.110%29.aspx [ ^ ]。



问题是:如何使用方法 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<>(string)<成功反序列化设计.NET类/ code>?它需要了解JSON代码如何映射到.NET集合和数组。您可以在我最近的答案中找到这个想法,其中包含一些源代码的插图:

如何将对象类型转换为C#类对象类型 [ ^ ]。



如果您对这种方法感兴趣但仍然怀疑将其应用于多级JSON数据的情况,那么欢迎您提出后续问题。



-SA
The class DataContractJSONSerializer is actually a very good option, but it perfectly works if you serialize using it in first place; then it will give you a Javascript/JSON sample you can further use to make input for deserialization on the .NET side.

It may be not perfectly convenient if data always originate in Javascript, or when the data is already given.

Another approach, especially good for Web development, is using the class System.Web.Script.Serialization.JavaScriptSerializer:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx[^].

The problem is: how to design a .NET class the way it would be successfully deserialize using the method System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<>(string)? It needs understanding of how JSON code is mapped to .NET collections and arrays. You can find the idea in my recent answer, containing illustration with some source code:
How To Convert object type to C# class object type[^].

If you are interested in such approach but still doubtful about applying it to the case of "multi-level JSON data", your follow-up questions will be welcome.

—SA


这篇关于如何将多级json数据转换为C#Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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