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?

11
  • typically you'd use obj['Result 1'] instead of obj->Result 1, whatever the equivalent would be in your chosen .net lang. Commented Jul 22, 2015 at 15:29
  • This is invalid JSON (maybe just a "typo" in the question?) Commented Jul 22, 2015 at 15:30
  • No this is not a typo. The field names are in quotes and valid. see stackoverflow.com/questions/5716792/json-fieldnames-spaces Commented Jul 22, 2015 at 15:31
  • Is there some fixed set of results, or could there be any number of them? Commented Jul 22, 2015 at 15:31
  • Here is a useful website for analyzing json. 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? Commented Jul 22, 2015 at 15:34

2 Answers 2

5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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());

1 Comment

However, what I would like to do is user the JsonConvert.DeserializeObject<T> method. However, I'm not sure what class structure I would need for the given JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.