Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return JSon object

Tags:

I am using a jQuery plugin that need a JSON object with following structure(I will be retrieving the values from database):

{ results: [     { id: "1", value: "ABC", info: "ABC" },     { id: "2", value: "JKL", info: "JKL" },     { id: "3", value: "XYZ", info: "XYZ" } ] } 

Here is my class:

public class results {     int _id;     string _value;     string _info;      public int id     {         get         {             return _id;         }         set         {             _id = value;         }     }     public string value     {         get         {             return _value;         }         set         {             _value = value;         }     }     public string info     {         get         {             return _info;         }         set         {             _info = value;         }     } } 

This is the way I serialize it:

results result = new results(); result.id = 1; result.value = "ABC"; result.info = "ABC"; string json = JsonConvert.SerializeObject(result); 

But this will return only one row. Can you please help me in returning more than one result? How can I get the result in the format specified above?

like image 600
user1640256 Avatar asked May 08 '13 13:05

user1640256


2 Answers

First of all, there's no such thing as a JSON object. What you've got in your question is a JavaScript object literal (see here for a great discussion on the difference). Here's how you would go about serializing what you've got to JSON though:

I would use an anonymous type filled with your results type:

string json = JsonConvert.SerializeObject(new {     results = new List<Result>()     {         new Result { id = 1, value = "ABC", info = "ABC" },         new Result { id = 2, value = "JKL", info = "JKL" }     } }); 

Also, note that the generated JSON has result items with ids of type Number instead of strings. I doubt this will be a problem, but it would be easy enough to change the type of id to string in the C#.

I'd also tweak your results type and get rid of the backing fields:

public class Result {     public int id { get ;set; }     public string value { get; set; }     public string info { get; set; } } 

Furthermore, classes conventionally are PascalCased and not camelCased.

Here's the generated JSON from the code above:

{   "results": [     {       "id": 1,       "value": "ABC",       "info": "ABC"     },     {       "id": 2,       "value": "JKL",       "info": "JKL"     }   ] } 
like image 161
Andrew Whitaker Avatar answered Oct 08 '22 04:10

Andrew Whitaker


You only have one row to serialize. Try something like this :

List<results> resultRows = new List<results>  resultRows.Add(new results{id = 1, value="ABC", info="ABC"}); resultRows.Add(new results{id = 2, value="XYZ", info="XYZ"});  string json = JavaScriptSerializer.Serialize(new { results = resultRows}); 
  • Edit to match OP's original json output

** Edit 2 : sorry, but I missed that he was using JSON.NET. Using the JavaScriptSerializer the above code produces this result :

{"results":[{"id":1,"value":"ABC","info":"ABC"},{"id":2,"value":"XYZ","info":"XYZ"}]} 
like image 32
ShaneBlake Avatar answered Oct 08 '22 04:10

ShaneBlake