【问题标题】:JsonConvert DeserializeObject can't find int membersJsonConvert DeserializeObject 找不到 int 成员
【发布时间】: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"
}

请注意,IDServiceStatus 字段位于 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


    【解决方案1】:

    问题在于,在您的 Json 中,您的属性名称格式错误(例如 "FreeMemory: " 而不是 "FreeMemory")。这一行:

    "{ … \"FreeMemory: \":50508,\"Malloc_Count: \":10050409,\"Free_Count: \":10049533, … }"
    

    应该是:

    "{ … \"FreeMemory\":50508,\"Malloc_Count\":10050409,\"Free_Count\":10049533, … }"
    

    或者,如果您实际上无法控制消息,则可以简单地更改 JsonProperty 属性:

    [JsonProperty("FreeMemory: ")]
    public int FreeMemory { get; set; }
    
    [JsonProperty("Malloc_Count: ")]
    public int MallocCount { get; set; }
    
    [JsonProperty("Free_Count: ")]
    public int FreeCount { get; set; }
    

    【讨论】:

    • 哇,非常感谢。我花了很长时间看这个,但没有看到那个!我无法控制我收到的消息,所以一定是服务器中的错误。添加与此错误 JSON 匹配的 JsonProperty 属性解决了该问题。
    • @SimonM 很高兴我能帮上忙。我建议让原始开发人员修复他们代码中明显的拼写错误,但如果这不是一个选项,是的,你可以只更改代码中的属性。为了完整起见,我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 2013-12-13
    • 2014-08-28
    • 2023-04-08
    相关资源
    最近更新 更多