Cannot deserialize the current JSON array (e.g. [1,2,3])
我正在尝试读取JSON结果。
这是我的RootObject
1 2 3 4 5 6 7 8 9 | public class RootObject { public int id { get; set; } public bool is_default { get; set; } public string name { get; set; } public int quantity { get; set; } public string stock { get; set; } public string unit_cost { get; set; } } |
这是我的JSON结果
1 | [{"id": 3636,"is_default": true,"name":"Unit","quantity": 1,"stock":"100000.00","unit_cost":"0"}, {"id": 4592,"is_default": false,"name":"Bundle","quantity": 5,"stock":"100000.00","unit_cost":"0"}] |
这是我的代码来读取结果
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static RootObject GetItems(string user, string key, Int32 tid, Int32 pid) { // Customize URL according to geo location parameters var url = string.Format(uniqueItemUrl, user, key, tid, pid); // Syncronious Consumption var syncClient = new WebClient(); var content = syncClient.DownloadString(url); return JsonConvert.DeserializeObject<RootObject>(content); } |
但是我有这个错误的问题:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'avaris_cash_salepoint.BL.UniqueItemDataRootObject' because the type
requires a JSON object (e.g. {"name":"value"}) to deserialize
correctly. To fix this error either change the JSON to a JSON object
(e.g. {"name":"value"}) or change the deserialized type to an array or
a type that implements a collection interface (e.g. ICollection,
IList) like List that can be deserialized from a JSON array.
JsonArrayAttribute can also be added to the type to force it to
deserialize from a JSON array. Path '', line 1, position 1.
在这一行:
返回JsonConvert.DeserializeObject(content);
对解决此错误有帮助吗?
我认为您遇到的问题是JSON是传入时的对象列表,并且与您的根类没有直接关系。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [ { "id": 3636, "is_default": true, "name":"Unit", "quantity": 1, "stock":"100000.00", "unit_cost":"0" }, { "id": 4592, "is_default": false, "name":"Bundle", "quantity": 5, "stock":"100000.00", "unit_cost":"0" } ] |
注意:利用http://jsonviewer.stack.hu/格式化JSON。
因此,如果您尝试以下操作,它应该可以工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static List<RootObject> GetItems(string user, string key, Int32 tid, Int32 pid) { // Customize URL according to geo location parameters var url = string.Format(uniqueItemUrl, user, key, tid, pid); // Syncronious Consumption var syncClient = new WebClient(); var content = syncClient.DownloadString(url); return JsonConvert.DeserializeObject<List<RootObject>>(content); } |
如果您不希望返回
我继续并在控制台应用程序中对此进行了测试,效果很好。
这是因为要获取的json是
然后,您必须更改方法以返回
如果您查看提供的响应的格式化版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [ { "id":3636, "is_default":true, "name":"Unit", "quantity":1, "stock":"100000.00", "unit_cost":"0" }, { "id":4592, "is_default":false, "name":"Bundle", "quantity":5, "stock":"100000.00", "unit_cost":"0" } ] |
您可以在那里看到两个实例。
您可以使用它来解决您的问题:
1 2 3 4 5 6 7 8 9 10 11 12 | private async void btn_Go_Click(object sender, RoutedEventArgs e) { HttpClient webClient = new HttpClient(); Uri uri = new Uri("http://www.school-link.net/webservice/get_student/?id=" + txtVCode.Text); HttpResponseMessage response = await webClient.GetAsync(uri); var jsonString = await response.Content.ReadAsStringAsync(); var _Data = JsonConvert.DeserializeObject <List<Student>>(jsonString); foreach (Student Student in _Data) { tb1.Text = Student.student_name; } } |
您有一个数组,将其转换为对象,类似于:
data: [{"id": 3636,"is_default": true,"name":"Unit","quantity": 1,"stock":"100000.00","unit_cost":"0"}, {"id": 4592,"is_default": false,"name":"Bundle","quantity": 5,"stock":"100000.00","unit_cost":"0"}]
To read more than one json tip (array, attribute) I did the following.
1 | var jVariable = JsonConvert.DeserializeObject<YourCommentaryClass>(jsonVariableContent); |
change to
1 | var jVariable = JsonConvert.DeserializeObject <List<YourCommentaryClass>>(jsonVariableContent); |
Because you cannot see all the bits in the method used in the foreach
loop.
Example foreach loop
1 2 3 4 5 6 | foreach (jsonDonanimSimple Variable in jVariable) { debugOutput(jVariable.Id.ToString()); debugOutput(jVariable.Header.ToString()); debugOutput(jVariable.Content.ToString()); } |
I also received an error in this loop and changed it as follows.
1 2 3 4 5 6 | foreach (jsonDonanimSimple Variable in jVariable) { debugOutput(Variable.Id.ToString()); debugOutput(Variable.Header.ToString()); debugOutput(Variable.Content.ToString()); } |
这可能是你
在尝试将您的json对象与另一个对象一起使用之前,只需检查api是否通过浏览器api / rootobject返回原始的json,就我而言,我发现基础数据提供程序mssqlserver没有运行并抛出了未处理的异常!
就如此容易 :)