开发者

converting an array in a json string into a list

开发者 https://www.devze.com 2023-03-03 12:51 出处:网络
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?

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号