admin管理员组

文章数量:1395185

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?

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?

Share Improve this question edited Feb 26, 2013 at 11:36 faizanjehangir asked Feb 26, 2013 at 11:30 faizanjehangirfaizanjehangir 2,8518 gold badges51 silver badges86 bronze badges 5
  • what is a desired output for valueList translated into JSON? – MarcinJuraszek Commented Feb 26, 2013 at 11:32
  • Please add your c# Json Convert code. Do you use JavascriptSerializer/DataContractSerializer/Newtosoft.JSON? – Murali Murugesan Commented Feb 26, 2013 at 11:33
  • values: { "M" : "Male", "F" : "Female" } – faizanjehangir Commented Feb 26, 2013 at 11:34
  • @Murali refer to the edit. – faizanjehangir Commented Feb 26, 2013 at 11:36
  • @faizanjehangir how are you calling server side function to fetch values. – Satpal Commented Feb 26, 2013 at 11:44
Add a ment  | 

1 Answer 1

Reset to default 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"}}

本文标签: javascriptParse C object list into key value pair in JSStack Overflow