4

I am getting JSON that is being returned from a REST web service for survey responses. It has arrays for the name portion of some of the name value pairs. Additionally the names will be variable depending on the type of questions asked. I'm using JSON.net and trying to deserialize the returned value into some type of object tree that I can walk but can't figure out what structure to use to have it filled in.

I tested the following snippet in LinqPad and fields is always empty. Is there someway to easily read in the variable data or do I have to parse it in code?

void Main() {
    string json = @"{
      'result_ok':true,
      'total_count':'51',
      'data':[{
        'id':'1',
        'status':'Deleted',
        'datesubmitted':'2015-01-12 10:43:47',
        '[question(3)]':'Red',
        '[question(4)]':'Blue',
        '[question(18)]':12,
        '[variable(\'STANDARD_IP\')]':'127.0.0.1',
        '[variable(\'STANDARD_GEOCOUNTRY\')]':'United States'
      }]
    }";
    var responses = JsonConvert.DeserializeObject<RootObject>(json);
    responses.Dump();
}

public class RootObject {
    public bool result_ok { get; set; }
    public string total_count { get; set; }
    public List<Response> data { get; set; }
}

public class Response {
    public string id { get; set; }
    public string status { get; set; }
    public string datesubmitted { get; set; }
    public List<object> fields = new List<object>();
}
4
  • I'm not entirely sure, but that doesn't even look like valid json to me. Commented Jan 20, 2015 at 0:39
  • jsoneditoronline.org and the .Net JSON visualizer seem to handle it ok. Commented Jan 20, 2015 at 0:41
  • 2
    While some libraries may handle it, single quotes around property names is off-spec. The spec requires double-quotes. Commented Jan 20, 2015 at 0:46
  • Try changing the type of data to Dictionary<string, object>[], and then convert each dictionary to a Response with your own logic. Commented Jan 20, 2015 at 0:53

1 Answer 1

5

Change the fields property in your Response class to be a Dictionary<string, object>, then mark it with a [JsonExtensionData] attribute like this:

public class Response
{
    public string id { get; set; }
    public string status { get; set; }
    public string datesubmitted { get; set; }
    [JsonExtensionData]
    public Dictionary<string, object> fields { get; set; }
}

All of the fields with the strange property names will then be placed into the dictionary where you can access them as normal. No extra code is required.

Here is a demo: https://dotnetfiddle.net/1rQUXT

Sign up to request clarification or add additional context in comments.

1 Comment

@Brain Rogers for the dotnetfiddle.net , didnt know that before. Very cool.

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.