.net c#反序列化包含数组的JSON数组?

Deserialize a JSON array that contains an array?
2021-07-31
  •  译文(汉语)
  •  原文(英语)

当我尝试使用.NET JavaScriptDeserializer反序列化以下内容时,出现错误消息: Type 'People' is not supported for deserialization of an array

当我使用JSON.NETlike时JsonConvert.DeserializeObject<List<People>>(args["xldata"]);,我得到了更详细的错误消息:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'People' 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<T> 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 '[0]', line 1, position 2.

我正在使用Linq-To-Entities,所以我认为我不能向其添加任何属性.

args["xldata"]是一个Dictionary以"xldata"为键,JSON下面的字符串为值的.

[ [ { "CarrierHeaderId": 17, "DetailHeaderId": 54, "tds_client": "0000000996", "carr_no": "0000019", "name": "John Doe ", "entry_type": "F", "carrier_type": "2", "scac_code": "1A ", "cert_no": "2d ", "address": "123 orange boulevard ", "city": "Orlando ", "state": "St", "zip": "33333 ", "phone": "9993023938", "epa_num": "123 ", "fein": "AA ", "host_carr_no": "BB ", "host_route_cd": "1a3 ", "country": "US", "CarrierHeaderLastUpdate": "6/11/2013 11:07 AM", "term_id": "TDSTES8", "trailer_req": "0", "truck_req": "0", "access_from": "1159", "access_to": "2359", "access_days": "YNNYYYN", "ins_exp_date": "12/20/13", "locked": "", "lockout_date": "", "lockout_reason": "", "st_license": "", "st_permit": "", "icc_permit": "", "liab_amt": "", "haz_mat_excl": "", "veh_liab_exp": "", "veh_liab_amt": "", "excess_liab_exp": "", "excess_liab_amt": "", "work_comp_exp": "", "work_comp_amt": "", "host_locked": "", "host_lockout_date": "", "host_lockout_reason": "", "po_relno_req": null, "general_exp": "", "general_amt": "", "access_profile": "", "max_load_amt": "", "own_consumption": "", "full_redirect": "", "det_required": "", "seal_processing": "", "email_group": "", "email_address": "", "shipment_origin": "", "auto_confirm": "", "bulk_transaction_picklist": "", "ExciseNo": "", "ERP_Carrier": "", "CarrierDetailLastUpdate": "6/11/2013 5:03 PM" } ] ]

解决过程1

JsonConvert.DeserializeObject<List<List<People>>> 应该管用.

When I try to deserialize the following with the .NET JavaScriptDeserializer, I get the error message: Type 'People' is not supported for deserialization of an array

When I use JSON.NET like JsonConvert.DeserializeObject<List<People>>(args["xldata"]);, I get a more detailed error message:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'People' 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<T> 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 '[0]', line 1, position 2.

I am using Linq-To-Entities, so I don't think I can add any attributes to it.

args["xldata"] is a Dictionary with "xldata" as the key and the JSON string below as the value.

[ [ { "CarrierHeaderId": 17, "DetailHeaderId": 54, "tds_client": "0000000996", "carr_no": "0000019", "name": "John Doe ", "entry_type": "F", "carrier_type": "2", "scac_code": "1A ", "cert_no": "2d ", "address": "123 orange boulevard ", "city": "Orlando ", "state": "St", "zip": "33333 ", "phone": "9993023938", "epa_num": "123 ", "fein": "AA ", "host_carr_no": "BB ", "host_route_cd": "1a3 ", "country": "US", "CarrierHeaderLastUpdate": "6/11/2013 11:07 AM", "term_id": "TDSTES8", "trailer_req": "0", "truck_req": "0", "access_from": "1159", "access_to": "2359", "access_days": "YNNYYYN", "ins_exp_date": "12/20/13", "locked": "", "lockout_date": "", "lockout_reason": "", "st_license": "", "st_permit": "", "icc_permit": "", "liab_amt": "", "haz_mat_excl": "", "veh_liab_exp": "", "veh_liab_amt": "", "excess_liab_exp": "", "excess_liab_amt": "", "work_comp_exp": "", "work_comp_amt": "", "host_locked": "", "host_lockout_date": "", "host_lockout_reason": "", "po_relno_req": null, "general_exp": "", "general_amt": "", "access_profile": "", "max_load_amt": "", "own_consumption": "", "full_redirect": "", "det_required": "", "seal_processing": "", "email_group": "", "email_address": "", "shipment_origin": "", "auto_confirm": "", "bulk_transaction_picklist": "", "ExciseNo": "", "ERP_Carrier": "", "CarrierDetailLastUpdate": "6/11/2013 5:03 PM" } ] ]

Solutions1

JsonConvert.DeserializeObject<List<List<People>>> should work.

转载于:https://stackoverflow.com/questions/17069869/deserialize-a-json-array-that-contains-an-array

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入