I've read a number of questions about this subject on here but never understood how to sort the problem.
When setting up the classes from JSON output with JSON2CSharp i get the following output as I have number values for each name with the public class __invalid_type__1 and __invalid_type__2, how do i over come this? I've looked at using Dictionary but don't understand fully how to implement this as I'm new to coding and c#, or is there a better way to do this?
[DataContract]
public class State
{
[DataMember]
public bool on { get; set; }
[DataMember]
public int bri { get; set; }
}
[DataContract]
public class __invalid_type__1
{
[DataMember]
public State state { get; set; }
[DataMember]
public string type { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
}
[DataContract]
public class State2
{
[DataMember]
public bool on { get; set; }
[DataMember]
public int bri { get; set; }
}
[DataContract]
public class __invalid_type__2
{
[DataMember]
public State2 state { get; set; }
[DataMember]
public string type { get; set; }
[DataMember]
public string name { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public __invalid_type__1 __invalid_name__1 { get; set; }
[DataMember]
public __invalid_type__2 __invalid_name__2 { get; set; }
}
my code for deserialzing Json is
public async static Task<RootObject> GetL ()
{
var http = new HttpClient();
var url = String.Format("http://192.168.0.71/api/apikey/");
var response = await http.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject)serializer.ReadObject(ms);
return data;
}
Original String
{
"1": {
"state": {
"on": true,
"bri": 254,
"alert": "select",
"reachable": true
},
"type": "Dimmable light",
"name": "Table Light",
"modelid": "LWB010",
"manufacturername": "Philips",
"uniqueid": "00:17:88:01:02:49:cf:db-0b",
"swversion": "1.15.2_r19181",
"swconfigid": "60083D2F",
"productid": "Philips-LWB010-1-A19DLv3"
},
"2": {
"state": {
"on": true,
"bri": 1,
"alert": "select",
"reachable": true
},
"type": "Dimmable light",
"name": "Dressing Room",
"modelid": "LWB010",
"manufacturername": "Philips",
"uniqueid": "00:17:88:01:02:44:60:94-0b",
"swversion": "1.15.2_r19181",
"swconfigid": "60083D2F",
"productid": "Philips-LWB010-1-A19DLv3"
}
}