I have a List that, when serialized as JSON gives me an array of objects. However, I need the serialized version to be structured as follows:
{
"item3":{
"id":3,
"name":"monkey"
},
"item4":{
"id":4,
"name":"turtle"
}
}
Currently, the JSON serialization is structured like this:
[
{
"id":3,
"name":"monkey"
},
{
"id":4,
"name":"turtle"
}
]
My goal is to be able to reference the array by item ID instead of numeric index (ie. arr["item3"].name instead of arr[0].name).
You might did that just putting the data into a dictionary is enough for JaveScripySerializer:
var dict = list.ToDictionary(
item => "item" + item.id);
(and serialize dict)
If not:
I don't have a PC handy for an example, but you should be able to:
You associate custom maps via RegisterConverters: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.registerconverters.aspx
Note you don't need to write a deserialize unless you need that too.
If you get stuck, I'll try to ad an example later.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.