Reading dynamic attributes of json into .net C#

烂漫一生 提交于 2019-12-08 02:38:12

问题


I am getting following json format after hitting to an API:

{
    "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": {
            "": []
        }
    } }

I am using JSON.Net to read the above given json data.

To map this data into .Net code, I will need classes in .net, having same properties' name as in json string. BUT there are some attributes in json which can be dynamic ("7407", "7408" etc in my case) i.e. this value can be changed based on what are we passing into parameters.

My question is, how can we map json attributes (which are dynamic in nature and can have any value depending upon the parameters provided to the apis) to our .net class ?


回答1:


You could map it to a Dictionary. Where your dynamic property is the Key of a DictionaryIem and the Objet is the Value of your DictionaryItem.

For Exmaple:

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; }
}

For this exmaple i simplified your json. so that it looks like this:

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



回答2:


Js object is a dictionary, so you can map to Dictionary with key = some attribute, and value - .Net code what you want

Dictionary<object,'your data class'>



回答3:


The best way I could find handling this kind of JSON data is by using JObject class of the JSON.Net library. For eg:

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

and then you can loop through or apply some other logic to read jSonObject



来源:https://stackoverflow.com/questions/19676295/reading-dynamic-attributes-of-json-into-net-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!