0

I am supposed to get the Json Object in specific format like

var dataObject = eval('[{"columns":[{ "title": "NAME"}, { "title": "COUNTY"}],"data":[["John Doe","Fresno"],["Billy","Fresno"],["Tom","Kern"],["King Smith","Kings"]]}]');

Till now I have always returned string from Server side in various format and consumed it in UI using Ajax. How to create Json object and send it to UI in the above shared format ?

JSON structure:

[{
    "columns": [{
        "title": "NAME"
    }, {
        "title": "COUNTY"
    }],
    "data": [
        ["John Doe", "Fresno"],
        ["Billy", "Fresno"],
        ["Tom", "Kern"],
        ["King Smith", "Kings"]
    ]
}]
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
Lara
  • 2,821
  • 7
  • 39
  • 72
  • 3
    Create a class on the server side that reflects the structure you need and then serialize it with [JavaScriptSerializer](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx) or even better use the [json.net](http://www.newtonsoft.com/json) serializer. – Matt Burland Jul 29 '16 at 14:41
  • Use 3rd party libraries for json converting like Json.Net - read their documentation about serializing to and from json or use built-in microsoft json serializer https://msdn.microsoft.com/en-us/library/system.web.helpers.json(v=vs.111).aspx – ams4fy Jul 29 '16 at 14:42
  • 1
    You shouldn't use eval here. Use JSON.parse or better, put an object literal there. – Tamas Hegedus Jul 29 '16 at 14:43
  • 2
    as a side note, the java function `eval` should be avoided whenever possible as it executes ANY text found as code, which is horribly insecure. `JSON.parse` is a much better alternative – pquest Jul 29 '16 at 14:43
  • 2
    duplicate of http://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp, http://stackoverflow.com/questions/14828520/how-to-create-my-json-string-by-using-c and probably several others – ADyson Jul 29 '16 at 14:44
  • @MattBurland I am having issue with creating the structure like I shared in the post. Could you pls help me how to add title and then data in the json object..I am aware of serialization – Lara Jul 29 '16 at 15:01
  • Please provide more details. What framework are you using to return your JSON object? [tag:wcf]? [tag:asp.net-mvc]? [tag:asp.net-web-api]? Something else? Or do you just need to save a JSON string to a file? – dbc Jul 29 '16 at 15:04
  • What's the issue? You need an object with a `columns` property, which is an array (or list) of objects with a `title` property. Then it also needs a `data` property which is an array of arrays of (presumably) strings. – Matt Burland Jul 29 '16 at 15:05
  • @dbc I am using Asp.net WebService – Lara Jul 29 '16 at 15:07

1 Answers1

2

You can use the JavaScriptSerializer class to do this for you.

var myObject = new MyModel();
var serializer = new JavaScriptSerializer();
var dataObject = serializer.Serialize(myObject);

Edit: Your model could look something like this:

public class MyModel
{
   List<string> Titles { get; set; }

   List<KeyValuePair> Data { get; set; }
}

Of course, you could use custom types for Titles and Data where you could define "title" properties (for Columns) and Name & City(?) properties (for Data). Using Lists (or whichever IEnumerable you prefer) will get you the structure you're looking for, though. Hope that helps!

Ricardo Reyna
  • 574
  • 6
  • 15