gpt4 book ai didi

c# - Newtonsoft JSON.net 反序列化错误,其中 JSON 中的字段更改顺序

转载 作者:行者123 更新时间:2023-12-01 22:08:53 24 4
gpt4 key购买 nike

这是一个从 Android 设备获取请求的 WCF 服务。相同的请求适用于 Lollipop 设备,而不适用于 jellybean 设备,因为 jellybean 在创建时以不同方式排列 JSON。

异常(exception):

Unexpected token when deserializing object: String. Path 'SearchFilters.config.$type', line 1, position 212.

非工作 Json:

{
"DeviceType": 2,
"SearchFilters": {
"config": {
"$values": [
{
"Collection": {
"DeviceType": 2
},
"Category": ""
}
],
"$type": "System.Collections.Generic.List`1[[Yoosh.SharedClasses.YooshConfig, YooshSharedClassesDll]], mscorlib"
}
},
"RequestingUserId": "66666666-6666-6666-6666-666666666666",
"APIKey": "xxx"
}

工作 Json:

{
"APIKey": "xxx",
"DeviceType": 2,
"RequestingUserId": "66666666-6666-6666-6666-666666666666",
"SearchFilters": {
"config": {
"$type": "System.Collections.Generic.List`1[[Yoosh.SharedClasses.YooshConfig, YooshSharedClassesDll]], mscorlib",
"$values": [
{
"Category": "",
"Collection": {
"DeviceType": 2
}
}
]
}
}
}

某些字段的顺序不同。这是唯一的区别。

C# 类:

public class QueryParameters 
{
BaseParameters m_baseParameters;
Guid m_gRequestingUserId;
Dictionary<string, object> m_SearchFilters;

[DataMember]
public string APIKey
{
get { return m_baseParameters.APIKey; }
set { m_baseParameters.APIKey = value; }
}

[DataMember]
public BaseParameters.YooshDeviceType DeviceType
{
get { return m_baseParameters.DeviceType; }
set { m_baseParameters.DeviceType = value; }
}

[DataMember]
public string DeviceId
{
get { return m_baseParameters.DeviceId; }
set { m_baseParameters.DeviceId = value; }
}

[DataMember]
public Guid RequestingUserId
{
get { return m_gRequestingUserId; }
set { m_gRequestingUserId = value; }
}

[DataMember]
public Dictionary<string, object> SearchFilters
{
get { return m_SearchFilters; }
set { m_SearchFilters = value; }
}
}

Json.net版本:6.0.8

最佳答案

设置JsonSerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead

根据documentation :

This sample deserializes JSON with MetadataPropertyHandling set to ReadAhead so that metadata properties do not need to be at the start of an object.

 string json = @"{
'Name': 'James',
'Password': 'Password1',
'$type': 'MyNamespace.User, MyAssembly'
}";

object o = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
// $type no longer needs to be first
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
});

请注意,此设置将 impact performance .

最后,在使用 TypeNameHandling 时,请注意 Newtonsoft docs 中的这一警告。 :

TypeNameHandling should be used with caution when your application deserializes JSON from an external source. Incoming types should be validated with a custom SerializationBinder when deserializing with a value other than None.

有关为什么需要这样做的讨论,请参阅 TypeNameHandling caution in Newtonsoft Json

关于c# - Newtonsoft JSON.net 反序列化错误,其中 JSON 中的字段更改顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com