I have an array in a json string like this:
"TheArray": "1,2,3,4,5"
What's the best way convert this into a list of int?
T开发者_Python百科hanks.
string theArray = "1,2,3,4,5";
List<int> theList = (
from string s in theArray.Split(',')
select Convert.ToInt32(s)
).ToList<int>();
Use JSON.Net or JavascriptSerializer, something like:
JArray jsonArray = JArray.Parse(jsonStr);
and then you can get out TheArray
from that.
The TheArray
in your question is not exactly a JSON array, so you will have to parse the string like other answers suggested after you get value of TheArray
http://www.nateirwin.net/2008/11/20/json-array-to-c-using-jsonnet/
http://james.newtonking.com/projects/json-net.aspx
Parsing JSON using Json.net
You can use the split method:
var items = myObj.TheArray.split(',');
items[0] == "1" // true
精彩评论