I am after something like this:
I have multiple lists in C# like so:
List<string> Names = new List<string>()
{
"Name1",
"Name2",
"Name3"
};
List<double> Values = new List<double>()
{
1,
2,
3
};
I want to serialize them into an array in javascript (I am actually looking to return a string) to get something like this:
[
{ name: "Name1", value: 1 },
{ name: "Name2", value: 2 },
{ name: "Name3", value: 3 }
]
So far I have tried this:
public class bmData
{
string _name;
public string name
{
get { return this._name; }
set { this._name = value; }
}
double _value;
public double value
{
get { return this._value; }
set { this._value = value; }
}
}
zip them together and wrap into a "data item" class. I want to keep this functionality for other reason. Then I zipped the key value pairs into this new class:
List<bmData> namesValues = Names.Zip(Values, (x, y) => new bmData{name = x, value = y}).ToList();
Finally I tried to serialize it like so:
var serializer = new JavaScriptSerializer();
string jsData = serializer.Serialize(namesValues);
But this return something like this:
[
{"name":"Name1","value":1},
{"name":"Name2","value":2},
{"name":"Name3","value":3}
]
It's those extra "" around name and value that are killing the key:value association. Any ideas?
Thank you!
stringtypes and as such are supposed to have double-quote characters around them. I.e. theJavaScriptSerializeris doing exactly what it's supposed to. Why do you think that the key names should not have double-quotes?