0

I have (or will have; its not written yet) a .NET web API controller method that I was just going to have return a list of ints as a JSON response. On the client side, I just wanted to deserialize the ints to a List. I tried to use { [1,2,3,4] }, but that's not valid JSON apparently. If I do { "ids" : [1,2,3,4] }, it doesn't deserialize to a simple list. I know I can create a super simple class for this, but I was hoping to avoid that. Is there a solution for this? Thanks!

var ids = JsonConvert.DeserializeObject<List<int>>(jsonResult1);
4

2 回答 2

9

如果你只想要一个简单的列表,你的 JSON 应该是一个简单的数组:

[1, 2, 3, 4]

那应该正确反序列化为List<int>

var ids = JsonConvert.DeserializeObject<List<int>>("[1, 2, 3, 4]");
于 2013-05-22T19:41:21.913 回答
0
    List<int> ids = new JavaScriptSerializer().Deserialize<List<int>>(jsonResult1);
于 2013-05-22T19:41:29.753 回答