How to build an literal array from list object?
I have the list object that I'm passing using json. How can I convert it to array like you can see below using jquery or javascript?
List<MyItem> items = new List<MyItems>();
items.Add(new (){ItemName = "Apple", ItemCount = 5});
items.Add(new (){ItemName = "Tomato", Ite开发者_高级运维mCount = 3});
items.Add(new (){ItemName = "Banana", ItemCount = 8});
items.Add(new (){ItemName = "Avokado", ItemCount = 5});
items.Add(new (){ItemName = "Potato", ItemCount = 9});
items.Add(new (){ItemName = "Onion", ItemCount = 1});
Array
var raw_data = [['Apple', 5],
['Tomato', 3],
['Banana', 8],
['Avokado', 5]];
You can use Json.Net to do this.
Also you can use javascriptserializer see this http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx
You can use a for loop to get the keys of an object and then use the keys returned in the for loop to associate the key with the value in the object:
var ob={x:1,y:2,z:3} for(var prop in ob){ alert(prop + ' = ' + ob[prop]); }
I think this will work (using the compiler in my brain - not trustworthy)
String[][] arrayize(List<MyItem> items)
{
List<String[]> answer = new List<String[]>;
foreach (var item in items)
{
answer.append(item.toArray()); // you need to write this api in MyItem
}
return answer.toArray();
}
精彩评论