【问题标题】:Reading dynamic attributes of json into .net C#将json的动态属性读入.net C#
【发布时间】:2013-10-30 07:30:58
【问题描述】:

点击 API 后,我得到以下 json 格式:

{
    "7407": {
        "survey_id": "406",
        "device_id": "1",
        "response_time": "2013-10-10 16:14:01",
        "timezone": "0",
        "language_id": "en",
        "response_id": "7407",
        "device_alias": "QR Code App",
        "site_name": "QR Code App",
        "country_name": "United States",
        "state_name": "New York",
        "city_name": "Suffern",
        "zip": "",
        "voucher_name": null,
        "voucher_mode": null,
        "surveyee_name": null,
        "surveyee_email": null,
        "surveyee_phone": null,
        "ques": {
            "": []
        }
    },
    "7408": {
        "survey_id": "406",
        "device_id": "1",
        "response_time": "2013-10-10 16:36:56",
        "timezone": "0",
        "language_id": "en",
        "response_id": "7408",
        "device_alias": "QR Code App",
        "site_name": "QR Code App",
        "country_name": "India",
        "state_name": "Gujarat",
        "city_name": "Ahmedabad",
        "zip": "",
        "voucher_name": null,
        "voucher_mode": null,
        "surveyee_name": null,
        "surveyee_email": null,
        "surveyee_phone": null,
        "ques": {
            "": []
        }
    } }

我正在使用 JSON.Net 来读取上面给出的 json 数据。

要将这些数据映射到 .Net 代码中,我需要 .net 中的类,其属性名称与 json 字符串中的名称相同。 但是 json 中有一些属性可以是动态的(在我的例子中是“7407”、“7408”等),即这个值可以根据我们传递给参数的内容来改变。

我的问题是,我们如何将 json 属性(本质上是动态的,并且可以根据提供给 api 的参数具有任何值)映射到我们的 .net 类?

【问题讨论】:

    标签: c# .net json json.net


    【解决方案1】:

    您可以将其映射到字典。您的动态属性是 DictionaryIem 的键,而 Objet 是 DictionaryItem 的值。

    例如:

    public class MyClass
    {
        public void readJson)
        {
            var json = "{\"7407\": {\"survey_id\": \"406\",\"device_id\": \"1\",},\"7408\": {\"survey_id\": \"406\",\"device_id\": \"1\",}}";
            Dictionary<int, MyObject> dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<int, MyObject>>(json);
            var count = dict.Keys.Count;
        }
    }
    
    public class MyObject
    {
        public string survey_id { get; set; }
        public string device_id { get; set; }
    }
    

    对于这个例子,我简化了你的 json。所以它看起来像这样:

    {
        "7407": {
            "survey_id": "406",
            "device_id": "1"
        },
        "7408": {
            "survey_id": "406",
            "device_id": "1"
        } 
    }
    

    【讨论】:

      【解决方案2】:

      Js 对象是一个字典,因此您可以使用 key = 某些属性和值映射到 Dictionary - .Net 代码您想要什么

      Dictionary<object,'your data class'>
      

      【讨论】:

      • Sorry Grundy.. 我想我们几乎同时回答了这个问题。我不想偷你的答案
      • @gluix,好的,你的回答更详细:-)
      【解决方案3】:

      我能找到处理这种 JSON 数据的最佳方法是使用 JSON.Net 库的JObject 类。 例如:

      Newtonsoft.Json.Linq.JObject jSonObject = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(somejsonstring);
      

      然后您可以循环或应用一些其他逻辑来读取jSonObject

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-22
        • 2017-05-10
        • 1970-01-01
        • 1970-01-01
        • 2011-07-13
        • 2023-04-07
        相关资源
        最近更新 更多