Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I am calling an API which is returning me below JSON response. Now to read the response I need to convert it into a class, So that it should be easily accessible for developer.
C#
{
    "root": {
        "123": {
            "license_id": 123,
            "name": "Praveen",
            "count": 4
        },
        "456": {
            "license_id": 456,
            "name": "Ashish",
            "count": 0
        }
    }
}


What would be the best way to read this JSON response. JSON reponse is returning list of license_ids and each license has few key,value pair and that I need to read.
I am stuck here because I don't know how do I convert this where license_id section has no name, instead it has a value.

I tried http://json2csharp.com/[^]and it returned below classes, which is I think not Okay.
public class __invalid_type__123
{
    public int license_id { get; set; }
    public string name { get; set; }
    public int count { get; set; }
}

public class __invalid_type__456
{
    public int license_id { get; set; }
    public string name { get; set; }
    public int count { get; set; }
}

public class Root
{
    public __invalid_type__123 __invalid_name__123 { get; set; }
    public __invalid_type__456 __invalid_name__456 { get; set; }
}

public class RootObject
{
    public Root root { get; set; }
}


Please help guys.
Posted
Updated 10-Feb-15 21:21pm
v3
Comments
Praveen Kumar Upadhyay 11-Feb-15 3:20am    
As You can see that conversion is not valid.

Try this - http://json2csharp.com/[^].
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 11-Feb-15 3:21am    
It's not happening. See my improved question.
First of all you have to have a class definition that can hold that data...
After that you should learn how to use Json.NET[^] library to serialize/deserialize objects...
 
Share this answer
 
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 11-Feb-15 4:27am    
No, it's not generating.

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