0

I'm getting a HttpResponse and trying to deserialize it by doing this:

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

The format is MsgPack. If I call the following code, it give me JSON like this:

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"
      ]
   ]
}

How should I proceed to deserialize this into a JSON array or dynamic object? Do I need to write a resolver that suits the structure of the message?

Would appreciate any help with this.

user2146441
  • 220
  • 1
  • 19
  • 43
  • 1
    "The format is MsgPack" - why not use a MessagePack deserializer/library? – Caramiriel Jun 21 '19 at 22:35
  • This json just looks extremely funky and malformed. – TheGeneral Jun 21 '19 at 22:35
  • 1
    @TheGeneral I think its supposed to deserialize to something like this: https://pastebin.com/ZNiMU7vd - not sure, never seen the format before – Caramiriel Jun 21 '19 at 22:41
  • @Caramiriel oh i see – TheGeneral Jun 21 '19 at 22:42
  • You can just deserialize to custom object.. Google json to c# and you'll find pages that will create c# objects for you. Then use newtonsoft or javascript serializer to turn the json into an object instance. – Heriberto Lugo Jun 21 '19 at 23:14
  • To get your objects: http://json2csharp.com . To deserialize https://www.newtonsoft.com/json or https://learn.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer?view=netframework-4.8 – Heriberto Lugo Jun 21 '19 at 23:16
  • This validates as JSON, but the structure isn't very familiar. It looks like a 'multi-key map', as in this question: https://stackoverflow.com/questions/33737992/how-to-pack-multi-key-map-with-msgpack-c – user2146441 Jun 22 '19 at 06:58

1 Answers1

1

You can use the JsonConvert.DeserializeObject Method from the Newtonsoft.Json nuget .

Notice it has a couple of overloads , a non generic ones which deserialize ur json to a .Net object , and generic ones which will try to convert you json to any type u have created