-3

I get the following JSON response.

[
  {
    "Issue": {
      "ID": 80,
      "Name": "Cold",
      "Accuracy": 90,
      "Icd": "J00",
      "IcdName": "Acute nasopharyngitis [common cold]",
      "ProfName": "Common cold",
      "Ranking": 1
    },
    "Specialisation": [
      {
        "ID": 15,
        "Name": "General practice",
        "SpecialistID": 0
      }
    ]
  }
]

I tried to follow the instructions given here. But I can't seem to fit that solution here. And in the documentation is explained only the scenario where you already have the class predefined. Any help?

JJJ
  • 32,902
  • 20
  • 89
  • 102

2 Answers2

5

Presumptions

Your question is not even clear. What are you asking? I presume you are asking how to make a C# class from that JSON?

Solution

Firstly, the JSON is an array (if its top-level tags are [] its an array itself. If its top level is {} then its a single object.

So what you have is an array returned with one result inside of it.

Going to json2csharp and pasting your code gives this:

public class Issue
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Accuracy { get; set; }
    public string Icd { get; set; }
    public string IcdName { get; set; }
    public string ProfName { get; set; }
    public int Ranking { get; set; }
}

public class Specialisation
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int SpecialistID { get; set; }
}

public class RootObject
{
    public Issue Issue { get; set; }
    public List<Specialisation> Specialisation { get; set; }
}

And you can see its created a RootObject almost indicating it is a single object, but you will need to deserialize this as a List<RootObject> not just RootObject.

So in C# that would be var result = JsonConvert.DeserializeObject<List<RootObject>>(theJsonString);

angelsix
  • 392
  • 1
  • 7
0

You should have a strongly typed c# class ready for your Json Response to be deserialized into... Json Utils has a generator to use, it came up with this:

public class Issue
{

    [JsonProperty("ID")]
    public int ID { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }

    [JsonProperty("Accuracy")]
    public int Accuracy { get; set; }

    [JsonProperty("Icd")]
    public string Icd { get; set; }

    [JsonProperty("IcdName")]
    public string IcdName { get; set; }

    [JsonProperty("ProfName")]
    public string ProfName { get; set; }

    [JsonProperty("Ranking")]
    public int Ranking { get; set; }
}

public class Specialisation
{

    [JsonProperty("ID")]
    public int ID { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }

    [JsonProperty("SpecialistID")]
    public int SpecialistID { get; set; }
}

public class RootObject
{

    [JsonProperty("Issue")]
    public Issue Issue { get; set; }

    [JsonProperty("Specialisation")]
    public IList<Specialisation> Specialisation { get; set; }
}

Then use

RootObject jsonAsCSharpClass = JsonConvert.DeserializeObject<RootObject>(jsonResponse);

I'm not 100% sure this class is good for you though, i have never some across a response where the root response is an array, without defining a root property first, see how it starts with "[" instead of { ??? You may want to also try

List<RootObject> jsonAsCSharpClass = JsonConvert.DeserializeObject<List<RootObject>>(jsonResponse);

Or wait for someone with more knowledge on the subject to come along...