1

I have a requirement to generate the following JSON using c# objects:

Right now Im using HttpResponseMessage (Web API) So I dont need any JSON.NET to do any extra conversion from my object.

returnVal = Request.CreateResponse(HttpStatusCode.OK, new Model.Custom.JsonResponse
                {
                    data = root,
                    success = true
                });

This is the JSON that I need to generate:

'data': [{
     'some var 1': 'value A',
     'some var 2': 'value B',
     'some var 3': 'value C'
 }, {
     'some var 1': 'value A',
     'some var 2': 'value B',
     'some var 3': 'value C'
 }, {
     'some var 1': 'value A',
     'some var 2': 'value B',
     'some var 3': 'value C'
 }]

Where 'some var' is dynamic.

Right now I was trying to use List<Dictionary<string,object>>(); but the problem is that with this approach I can only generate this:

 'data': [{
     'some var 1': 'value A'
 }, {
     'some var 1': 'value A'
 }, {
     'some var 1': 'value A'
 }]

My actual classes look like this:

public class RootObject {
    public bool success {
        get;
        set;
    }
    public List < Dictionary < string, object >> jsonData {
        get;
        set;
    }
}

var root = new RootObject();

root.jsonData = new List < Dictionary < string, object >> ();

// create new record
var newRecord = new Dictionary < string,object > ();
newRecord.Add("1", "H"); // 1 = some var 1, H = value A 
// add record to collection
root.jsonData.Add(newRecord);

// create new record
newRecord = new Dictionary < string, object > ();
newRecord.Add("5", "L");
// add record to collection
root.jsonData.Add(newRecord);

So this will output:

 'data': [{
     '1': 'H'
 }, {
     '5': 'L'
 }]

Any clue? Thanks

VAAA
  • 14,531
  • 28
  • 130
  • 253

3 Answers3

2
public class MyClass
{
    public List<JObject> data = new List<JObject>();
}

var m = new MyClass();
m.data.Add(new JObject());
m.data.Add(new JObject());

m.data[0]["some var 1"] = "value A";
m.data[0]["some var 2"] = "value B";
m.data[0]["some var 3"] = "value C";

m.data[1]["some var 1"] = "value A";
m.data[1]["some var 2"] = "value B";
m.data[1]["some var 3"] = "value C";

var json = JsonConvert.SerializeObject(m);

OUTPUT

{
  "data": [
    {
      "some var 1": "value A",
      "some var 2": "value B",
      "some var 3": "value C"
    },
    {
      "some var 1": "value A",
      "some var 2": "value B",
      "some var 3": "value C"
    }
  ]
}
L.B
  • 114,136
  • 19
  • 178
  • 224
0

You just need to add more elements to your dictionary:

newRecord.Add("1", "H");
newRecord.Add("2", "I");
newRecord.Add("3", "J");
Mike Hixson
  • 5,071
  • 1
  • 19
  • 24
  • 1
    This will just output {"1":"H"},{"2":"I"},{"3":"J"} instead of {"1":"H","2":"I", "3":"J"} – VAAA Jul 02 '14 at 18:15
  • Because after newRecord I have to add the newRecord to the Dictionary List. – VAAA Jul 02 '14 at 18:16
  • 1
    Looks like you are getting the behavior of the crappy DataContractJsonSerializer. You may want to try swapping it out for JSON.NET instead: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx – Mike Hixson Jul 02 '14 at 18:28
0

This is what I get when I paste your JSON into Visual Studio "as JSON":

public class Rootobject
{
    public Datum[] data { get; set; }
}

public class Datum
{
    public string somevar1 { get; set; }
    public string somevar2 { get; set; }
    public string somevar3 { get; set; }
}
Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34