【发布时间】:2017-01-30 15:10:09
【问题描述】:
我有一个奇怪的情况,我的其他类正在正确反序列化,但是这个具有三个 int 值的类没有正确反序列化。使用跟踪编写器设置,我看到 Json.Net 报告它找不到成员,但我不确定为什么。
类:
public class BroadcastMemoryResponse : BaseResponse
{
public int FreeMemory { get; set; }
[JsonProperty("Malloc_Count")]
public int MallocCount { get; set; }
[JsonProperty("Free_Count")]
public int FreeCount { get; set; }
}
JSON:
{
"ID": 100,
"Service": "BroadcastMemory",
"FreeMemory: ": 50508,
"Malloc_Count: ": 10050409,
"Free_Count: ": 10049533,
"Status": "Success"
}
请注意,ID、Service 和 Status 字段位于 BaseResponse 类中,并且这些字段已成功反序列化(ID 是一个 int 并且可以正确反序列化)。当 JSON 映射到 C# 时,我使用 JsonProperties 删除 JSON 中的下划线。类和属性都是公开的,getter/setter 也是公开的,所以不确定问题是什么..?
测试反序列化代码:
ITraceWriter traceWriter = new MemoryTraceWriter();
var settings = new JsonSerializerSettings { TraceWriter = traceWriter };
string str = "{\"ID\":100,\"Service\":\"BroadcastMemory\",\"FreeMemory: \":50508,\"Malloc_Count: \":10050409,\"Free_Count: \":10049533,\"Status\":\"Success\"}";
var deserializedObj = JsonConvert.DeserializeObject<BroadcastMemoryResponse>(str, settings);
跟踪器输出:
2017-01-30T09:49:46.807 Info Started deserializing TcpClient.Core.Model.BroadcastMemoryResponse. Path 'ID', line 1, position 6.
2017-01-30T09:49:46.836 Verbose Could not find member 'FreeMemory: ' on TcpClient.Core.Model.BroadcastMemoryResponse. Path '['FreeMemory: ']', line 1, position 51.
2017-01-30T09:49:46.838 Verbose Could not find member 'Malloc_Count: ' on TcpClient.Core.Model.BroadcastMemoryResponse. Path '['Malloc_Count: ']', line 1, position 74.
2017-01-30T09:49:46.838 Verbose Could not find member 'Free_Count: ' on TcpClient.Core.Model.BroadcastMemoryResponse. Path '['Free_Count: ']', line 1, position 98.
2017-01-30T09:49:46.840 Info Finished deserializing TcpClient.Core.Model.BroadcastMemoryResponse. Path '', line 1, position 126.
2017-01-30T09:49:46.841 Verbose Deserialized JSON:
{
"ID": 100,
"Service": "BroadcastMemory",
"FreeMemory: ": 50508,
"Malloc_Count: ": 10050409,
"Free_Count: ": 10049533,
"Status": "Success"
}
【问题讨论】:
标签: c# json serialization json.net