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
1 Answer
Reset to default 7You 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
版权声明:本文标题:javascript - Parse C# object list into key value pair in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744596210a2614793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论