3

I have a JSON object constituting of column model attributes of a grid. I want to populate a dropdown in the grid, for that I have a object list with ID - Value pair.

The grid model takes the values in the following format:

values: { "be": "Belgium", "fr": "France", "uk": "Great-Britain", "nl": "Nederland" }

My anonymous object structure is the following:

List<Object> valueList = new List<Object>();
var item1 = new { ID = "M", Value = "Male" };
var item2 = new { ID = "F", Value = "Female" };
valueList.Add(item1);
valueList.Add(item2);

The array structure after $.parseJSON is:

    [
Object
ID: "M"
Value: "Male"
__proto__: Object
, 
Object
ID: "F"
Value: "Female"
__proto__: Object
]

EDIT:

Using this for json converter:

var jsonSerialiser = new JavaScriptSerializer();
json = jsonSerialiser.Serialize(model);
return json;

Where model is a list with other grid attributes and List of values.

How would I construct a JSON formatted data from this, such that I have similar results? Is there an proper way to it? Or would I have to do something similar to splitting and making a string out of it?

5
  • what is a desired output for valueList translated into JSON? Commented Feb 26, 2013 at 11:32
  • Please add your c# Json Convert code. Do you use JavascriptSerializer/DataContractSerializer/Newtosoft.JSON? Commented Feb 26, 2013 at 11:33
  • values: { "M" : "Male", "F" : "Female" } Commented Feb 26, 2013 at 11:34
  • @Murali refer to the edit. Commented Feb 26, 2013 at 11:36
  • @faizanjehangir how are you calling server side function to fetch values. Commented Feb 26, 2013 at 11:44

1 Answer 1

7

You can use a dictionary:

Dictionary<string, string> valueList = new Dictionary<string, string>();
valueList.Add("M", "Male");
valueList.Add("F", "Female");

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(new { values: valueList });
return json;

This will serialize as:

{"values":{"M":"Male","F":"Female"}}
Sign up to request clarification or add additional context in comments.

Comments

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.