【问题标题】:Deserializing custom dictionary反序列化自定义字典
【发布时间】:2019-06-21 22:31:18
【问题描述】:

我得到一个 HttpResponse 并试图通过这样做来反序列化它:

response = (HttpWebResponse)request.GetResponse();
Stream objStream = response.GetResponseStream();
BinaryReader breader = new BinaryReader(objStream);
byte[] buffer = breader.ReadBytes((int)response.ContentLength);

格式为 MsgPack。如果我调用以下代码,它会给我这样的 JSON:

var unpackKNN = MessagePack.MessagePackSerializer.ToJson(buffer);

Json

{  
   "__schema":{  
      "__level0":"result|status",
      "__level1":"token|status|total|amount|details|id|name|category|cat|group",
      "__level2":"method|code|timestamp|result"
   },
   "data":[  
      "__level0",
      [  
         "__level1",
         "abcd",
         "true",
         100,
         200,
         "xyz",
         12345,
         "Giraffe",
         "1",
         "One",
         "First"
      ],
      [  
         "__level2",
         "request",
         "SUCCESS",
         15000000000,
         "Success"
      ]
   ]
}

我应该如何将其反序列化为 JSON 数组或动态对象?我是否需要编写适合消息结构的解析器?

在这方面的任何帮助将不胜感激。

【问题讨论】:

  • “格式是 MsgPack” - 为什么不使用 MessagePack 反序列化器/库?
  • 这个 json 看起来非常时髦而且格式不正确。
  • @TheGeneral 我认为它应该反序列化为这样的东西:pastebin.com/ZNiMU7vd - 不确定,以前从未见过这种格式
  • @Caramiriel 哦,我明白了
  • 您可以反序列化为自定义对象。Google json 到 c#,您会找到将为您创建 c# 对象的页面。然后使用 newtonsoft 或 javascript 序列化器将 json 转换为对象实例。

标签: c# deserialization msgpack


【解决方案1】:

您可以使用 Newtonsoft.Json nuget 中的 JsonConvert.DeserializeObject 方法。

请注意它有几个重载,一个非通用的将你的 json 反序列化为 .Net 对象,而通用的将尝试将你的 json 转换为你创建的任何类型

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多