项目场景:

前后端分离项目


问题描述

前端向后端post数据时,后段报:
{“result”:null,“targetUrl”:null,“success”:false,“error”:{“code”:0,“message”:“Your request is not valid!”,“details”:“The following errors were detected during validation.\r\n - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type ‘System.Collections.Generic.List1[AbpVue.Dto.WindingSummary_ScanResult]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'scanResult', line 1, position 14.\r\n","validationErrors":[{"message":"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List1[AbpVue.Dto.WindingSummary_ScanResult]’ because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath ‘scanResult’, line 1, position 14.”,“members”:[“scanResult”]}]},“unAuthorizedRequest”:false,“__abp”:true}

原因分析:

后端要求参数为:

List<WindingSummary_ScanResult> scanResult

对应json

[{\"LotNum\":\"N111932126048\",\"MaterialNum\":\"C6004120T 130N211I\",\"Weight\":0.2254,\"Unit\":\"KG\"},{\"LotNum\":\"N111932126048\",\"MaterialNum\":\"C6001240T 140N121I\",\"Weight\":0.22179,\"Unit\":\"KG\"},{\"LotNum\":\"N112119326048\",\"MaterialNum\":\"C60111P 500D600Z\",\"Weight\":0.001,\"Unit\":\"KG\"}]

但实际传参:

{"scanResult":"[{\"LotNum\":\"N11191326048\",\"MaterialNum\":\"C600140T 130N211I\",\"Weight\":0.2254,\"Unit\":\"KG\"},{\"LotNum\":\"N11193226048\",\"MaterialNum\":\"C600140T 140N121I\",\"Weight\":0.2719,\"Unit\":\"KG\"},{\"LotNum\":\"N11193226048\",\"MaterialNum\":\"C601111P 500D600Z\",\"Weight\":0.0201,\"Unit\":\"KG\"}]"}

导致后端无法序列化为对象。


解决方案:

这里我对原来的参数List外面包了一层:

public class WindingSummary_ScanResultDTO 
    {
        public List<WindingSummary_ScanResult> scanResult { get; set; }
    }

如果有更好的处理方法欢迎提出!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐