将JSON映射到C#类 [英] Mapping json to C# class

查看:87
本文介绍了将JSON映射到C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次将数据从Web服务映射到C#类,但是我被卡在此数据源上.对象是根对象内的数据列表.当列表位于对象内部时,映射没有问题,但是当它是根级别的列表时,我无法获取它.我可以访问json或xml格式的源数据.

json有效,如http://jsonlint.com/
所示
这仅仅是.NET无法实现的事情吗?
至少不使用这样的内容 [

I have mapped data from a webservice to a C# class many times, but I''m stuck on this data source. An object is a list of data within the root object. I have no problems with mapping when a list is inside of an object, but I can''t get it when it is a list at the root level. I can access source data in json or xml format.

The json is valid as shown by http://jsonlint.com/

Is this simply something that is not possible in .NET?
At least without using something like this [^]. It seems this was created for this problem.

Sample of data (can''t provide link or actual data as it is API data that requires license). Not needed are not needed and not the cause of problems, the "needed" data is failing (I can get first data in series fine, by mapping directly to 2 strings.):

{
    "notneeded1": {
        "sample1": "0",
        "sample2": "2"
    },
    "notneeded2": {
        "sampleA": "1",
        "sampleB": "2"
    },
    "needed": [
        {
            "id": "SymbolA",
            "time": "2012/10/10"
        },
        {
            "id": "SymbolB",
            "time": "2012/10/10"
        },
        {
            "id": "SymbolC",
            "time": "2012/10/10"
        }
        ]
}

推荐答案

使用 FastJSON [ ^ ],我很确定它将完成这项工作为你.如果该类未知,您将获得通用集合.
Use FastJSON[^], I am pretty sure it will do the job for you. If the class is unknown, you will get generic collections.


我建​​议使用 Data Contract System.Runtime.Serialization.Json.DataContractJsonSerializer:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx [ ^ ].

请同时参阅我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
I would recommend to use Data Contract and System.Runtime.Serialization.Json.DataContractJsonSerializer:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^].

Please see also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


SAK,我最终选择了另一条路线.我编写并测试了它,然后看到您已经回答了,所以我还没有尝试您的版本.

SAK, I ended up going a different route. I wrote and tested it, then saw you had answered so I didn''t give your version a try, yet.

        public RootObject ExecuteUpdate(RestRequest request, string url)
        {   
            RestClient client = new RestClient();
            client.BaseUrl = url;
            IRestResponse response1 = client.Execute(request);
            string json = response1.Content;
            UpdateObjects.RootObject root = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<rootobject>(json);
            return root;
        }
</rootobject>


这篇关于将JSON映射到C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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