0

I have json object returned from another application, that i have no control over it and the structure of each object is different ,but i want to extract the same data from each object with its title (I am using NewtownSoft):

{
"myData": [
{
  "one": {
    "in": 0,
    "out": 17,
    "total": 17
  },
  "two": {
    "total": 17
  },
  "three": {
    "total": 0
  },
  "four": {
    "total": 8
  },
  "five": {
    "total": 0
  },
  "six": {
    "total": 0
  },
  "seven": {
    "total": 0
  }
}  ]}

i want the result to be as in this image

enter image description here

and deserialize this code using only one class

public class Example{
public string number {get;set;}
public int total {get; set;}
}
Hossam Elsagheer
  • 151
  • 1
  • 5
  • 15
  • 4
    Possible duplicate of [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – rüff0 Jul 15 '17 at 04:12

1 Answers1

0

If you have control over the JSON that is generated, then modify the JSON:

{
"myData": [
{
  "Example": {
    "number": "one",
    "in": 0,
    "out": 17,
    "total": 17
  },
  "Example": {
    "number": "two",
    "total": 17
  },
  "Example": {
    "number": "three",
    "total": 0
  },
  "Example": {
    "number": "four",
    "total": 8
  },
  "Example": {
    "number": "five",
    "total": 0
  },
  "Example": {
    "number": "six",
    "total": 0
  },
  "Example": {
    "number": "seven",
    "total": 0
  }
}  ]}

C# Classes:

public class Example
{
    public string number { get; set; }
    public int total { get; set; }
}

public class MyData
{
    public Example Example { get; set; }
}

public class RootObject
{
    public List<MyData> myData { get; set; }
}
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52