3

I'm working with a third party system that returns JSON.

I'm trying to work out how to deserialise the following json;

{"getResponse": {
    "Results": {
        "Result 1": {"Row": [{Name:Somename}]
     }
}

I'm using the Newtonsoft JSON library. Anyone know how I can parse this into .Net objects?

djnz
  • 109
  • 1
  • 14
  • typically you'd use `obj['Result 1']` instead of `obj->Result 1`, whatever the equivalent would be in your chosen .net lang. – Marc B Jul 22 '15 at 15:29
  • This is invalid JSON (maybe just a "typo" in the question?) – Felix Kling Jul 22 '15 at 15:30
  • No this is not a typo. The field names are in quotes and valid. see http://stackoverflow.com/questions/5716792/json-fieldnames-spaces – djnz Jul 22 '15 at 15:31
  • Is there some fixed set of results, or could there be any number of them? – dbc Jul 22 '15 at 15:31
  • Here is a useful website for analyzing json. http://www.jsoneditoronline.org/ You can see it does not like your snippet. Are you generating this json or is it being provided from an external source? – Doug E Fresh Jul 22 '15 at 15:34
  • Fixed. One field named 'Result 1'. Within this there is the Row array. – djnz Jul 22 '15 at 15:34
  • This is being provided by an external source I don't have control over. Fiddler thinks this is valid – djnz Jul 22 '15 at 15:37
  • http://jsonlint.com/ also complains. `Name:Somename` needs to be `"Name": "Somename"`. Also, there needs to be an `} }` at the end to match the opening and closing curly braces. – dbc Jul 22 '15 at 15:40
  • If your JSON were valid, this would be a duplicate of http://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers – dbc Jul 22 '15 at 15:54
  • I omitted the exact json because it is too long. you are correct I missed a closing brace. Also, the name field should be enclosed in quotes as pointed out. However, the space is valid and the answer to that question does not help. – djnz Jul 22 '15 at 16:05
  • Currently I can access the list I am interested in like this JObject result = JObject.Parse(jobStatusResponse1); dynamic t = result["getResponse"]["Results"]["Result 1"]["Row"]; However, I still cannot parse the array. – djnz Jul 22 '15 at 16:06

2 Answers2

4

To parse your JSON into objects using JsonConvert.DeserializeObject<T> you can make your class structure like this:

public class RootObject
{
    public GetResponse getResponse { get; set; }
}

public class GetResponse
{
    public Results Results { get; set; }
}

public class Results
{
    [JsonProperty("Result 1")]
    public Result1 Result1 { get; set; }
}

public class Result1
{
    [JsonProperty("Row")]
    public List<Row> Rows { get; set; }
}

public class Row
{
    public string Name { get; set; }
}

Then deserialize like this:

string json = @"
{
    ""getResponse"": {
        ""Results"": {
            ""Result 1"": {
                ""Row"": [
                    {
                        ""Name"": ""Somename""
                    }
                ]
            }
        }
    }
}";

RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
foreach (Row row in root.getResponse.Results.Result1.Rows)
{
    Console.WriteLine(row.Name);
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
1

I hope there's a better way of doing this so I'm posting this in the hope someone will provide a better answer.

Given the below (corrected) JSON;

{"getResponse": {
    "Results": {
        "Result 1": {"Row": [{"Name":"Somename"}]
     }
  }
}

I want to deserialize the elements in the Row array and not sure how to do this with a custom converter.

So my solution till I have the time to find a better way is this;

JObject result = JObject.Parse(response);
var t = result["getResponse"]["Results"]["Result 1"]["Row"];
var els =                 
        JsonConvert.DeserializeObject<List<MyResponse>>(t.ToString());
djnz
  • 109
  • 1
  • 14
  • However, what I would like to do is user the JsonConvert.DeserializeObject method. However, I'm not sure what class structure I would need for the given JSON. – djnz Jul 23 '15 at 09:16