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

0

3 Answers 3

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

Comments

0

You just need to add more elements to your dictionary:

newRecord.Add("1", "H");
newRecord.Add("2", "I");
newRecord.Add("3", "J");

3 Comments

This will just output {"1":"H"},{"2":"I"},{"3":"J"} instead of {"1":"H","2":"I", "3":"J"}
Because after newRecord I have to add the newRecord to the Dictionary List.
Looks like you are getting the behavior of the crappy DataContractJsonSerializer. You may want to try swapping it out for JSON.NET instead: blogs.msdn.com/b/henrikn/archive/2012/02/18/…
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; }
}

2 Comments

That wont be dynamic. Because the Class has the variable name fixed.
Looks like @L.B. has your answer then.

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.