Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, im trying to deserialize the following JSON String:

http://docs.ninja.is/rest/device.html#device-get-devices[^]

C#
{
  "result": 1,
  "error": null,
  "id": 0,
  "data": {
    "ASTEALTHYNODE01_0301_0_30": {
        "css_class": "sensor rf digital humidity",
        "default_name": "Humidity",
        "device_type": "humidity",
        "did": "30",
        "gid": "0301",
        "has_subdevice_count": 0,
        "has_time_series": 1,
        "is_actuator": 0,
        "is_sensor": 1,
        "is_silent": 0,
        "last_data": {
            "DA": 58,
            "timestamp": 1355791804474
        },
        "meta": {},
        "node": "ASTEALTHYNODE01",
        "shortName": "Humidity",
        "subDevices": {},
        "vid": "0"
    },
    "ASTEALTHYNODE01_0301_0_31": {
        "css_class": "sensor rf digital temperature",
        "default_name": "Temperature",
        "device_type": "temperature",
        "did": "31",
        "gid": "0301",
        "has_subdevice_count": 0,
        "has_time_series": 1,
        "is_actuator": 0,
        "is_sensor": 1,
        "is_silent": 0,
        "last_data": {
            "DA": 26.6,
            "timestamp": 1355791804475
        },
        "meta": {},
        "node": "ASTEALTHYNODE01",
        "shortName": "Temperature",
        "subDevices": {},
        "vid": "0"
    },
    "ASTEALTHYNODE01_0_0_1000": {
        "css_class": "actuator cape led rgbled",
        "default_name": "On Board RGB LED",
        "device_type": "rgbled",
        "did": "1000",
        "gid": "0",
        "has_subdevice_count": 0,
        "has_time_series": 0,
        "is_actuator": 1,
        "is_sensor": 1,
        "is_silent": 0,
        "last_data": {
            "DA": "22B42B",
            "timestamp": 1355790209080
        },
        "meta": {},
        "node": "ASTEALTHYNODE01",
        "shortName": "On Board RGB LED",
        "subDevices": {},
        "vid": "0"
    },
    "ASTEALTHYNODE01_0_0_11": {
        "css_class": "sensor serial rf rf433 receiver transmitter",
        "default_name": "RF 433Mhz",
        "device_type": "rf433",
        "did": "11",
        "gid": "0",
        "has_subdevice_count": 1,
        "has_time_series": 0,
        "is_actuator": 1,
        "is_sensor": 1,
        "is_silent": 0,
        "last_data": {
            "DA": "010001010101010100010101",
            "timestamp": 1355789891324
        },
        "meta": {},
        "node": "ASTEALTHYNODE01",
        "shortName": "RF 433Mhz",
        "subDevices": {
            "6l8At": {
                "category": "rf",
                "data": "011111110001010100110000",
                "shortName": "Door Bell",
                "type": "sensor"
            }
        },
        "vid": "0"
    }
  }
}


Usually I create the classes with help of: http://json2csharp.com/[^] and then I am doing something like this (Json.NET libary):
C#
var result = JsonConvert.DeserializeObject<MyObject>(jsonString);


But the number of nodes and their names (example: ASTEALTHYNODE01_0_0_11) are unknown before I get the JSON string. How can i deserialize this ?

Thank you
Posted
Updated 19-Dec-13 22:27pm
v5
Comments
Sergey Alexandrovich Kryukov 20-Dec-13 4:12am    
Is the format given, or you can change it (serialize yourself). Then it will be really, really easy.
You see, the serialization and deserialization code should come together, otherwise programming becomes the clown tricks... :-)
—SA
Member 10336798 20-Dec-13 4:56am    
I am doing a HTTP Request and get the JSON you can see above. This JSON isn't generated by me, so i cant't change it. This is a list of devices (like temperature sensor, rf transmitter...) and I want to create an array of type 'device' from this list.

1 solution

You go ahead and use Json.Net to deserialize this JSON, it's deserialized very well.

check it here:[^]
is not working well... check out this one:
http://json.parser.online.fr/[^]

HTH!
 
Share this answer
 
Comments
Member 10336798 20-Dec-13 8:30am    
Thanks for your reply but I don't understand your solution. I want to deserialize this string to get a C# class. But I don't know how it's done with Json.Net. It works with simple Json strings but not with this one, because I don't know how many data items i get and how they are called.

I used http://json2csharp.com/ to create simple C# classes. Your link does not provide this fuctionality.
Member 10336798 20-Dec-13 8:38am    
I want to do something like this:

class Program
{
static void Main(string[] args)
{
string json = "{long json sting:.......}";

Devices devices = JsonConvert.DeserializeObject-devices-(json);
}
}


class Devices
{
public string result { get; set; }
public string error { get; set; }
public string id { get; set; }
public Device[] data { get; set; }
//...
}

class Device
{
public string css_class { get; set; }
public string default_name { get; set; }
public string device_type { get; set; }
public string did { get; set; }
//...
}
Sunny_Kumar_ 20-Dec-13 10:08am    
how about using dynamic variable to get the complex JSON deserialized:
Get Newtonsoft.Json from here http://json.codeplex.com/
dynamic jsonObj = JsonConvert.DeserializeObject(jsonStr);
Console.WriteLine(jsonObj.data.ASTEALTHYNODE01_0301_0_30.css_class);

HTH!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900