shrekDeep
shrekDeep

Reputation: 2328

Reading dynamic attributes of json into .net C#

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 ?

Upvotes: 2

Views: 1326

Answers (3)

shrekDeep
shrekDeep

Reputation: 2328

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

Upvotes: 0

tveng
tveng

Reputation: 200

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

Upvotes: 3

Grundy
Grundy

Reputation: 13381

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'>

Upvotes: 0

Related Questions