-2
{
    "0": {
        "no": "tenon",
        "title": "ProdtesterTITLE439",
        "stock": 12
    },
    "success": 1
}

I want to desrialize those json.The problem is I cant create class with name 0 in c sharp.i have tried

[JsonObject(Title = "0")]

and

[DataContract(Name ="0")]

Not one of them are worked.


TheGeneral
  • 79,002
  • 9
  • 103
  • 141
kaushalya
  • 17
  • 4

1 Answers1

3

Good news! Your root object doesn't have a name, so you don't need to create a class with that name. 0 is a property of the root object.

Of course, 0 isn't a valid property name in C# either. That's where JsonPropertyAttribute comes in:

public class RootObject
{
    [JsonProperty("0")]
    public MyData Data {get;set;}
    public bool Success {get;set;}
}

public class MyData
{
    public int Stock {get;set;}
    // other properties
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86