I'm trying to parse byte[]
array to Dictionary<String,Object>
using Json.Net but with no success
Actually I'm in doubt about its possibility. So is it possible? with this library or with anyother library?
here is what I have tried but DeserializeObject excepts only string as parameter afaik
public static Dictionary<String, Object> parse(byte[] json){
Dictionary<String, Object> values = JsonConvert.DeserializeObject<Dictionary<String, Object>>(json);
return values;
}
JSON does not support that. Use Base64. That is your library supporting it, not JSON itself. The byte array wont be stored as byte array in the JSON, JSON is a text format meant to be human readable.
Once you have the bytes as a string, you can use the JSON. dumps method to convert the string object to JSON.
The char type in Universal Binary JSON is an unsigned byte meant to represent a single printable ASCII character (decimal values 0-127). Put another way, the char type represents a single-byte UTF-8 encoded character.
Text. Json. Serialization namespace, which contains attributes and APIs for advanced scenarios and customization specific to serialization and deserialization.
Is the byte[]
some sort of encoded text? If so, decode it first, e.g. if the encoding is UTF8:
public static Dictionary<String, Object> parse(byte[] json){
string jsonStr = Encoding.UTF8.GetString(json);
return JsonConvert.DeserializeObject<Dictionary<String, Object>>(jsonStr);
}
To understand what's in the byte[] you should specify the encoding and use an method that could actually get byte[].
As I'm not aware of such method this will be the solution for your problem -
So the correct way to do it will be -
public static Dictionary<String, Object> parse(byte[] json)
{
var reader = new StreamReader(new MemoryStream(json), Encoding.Default);
Dictionary<String, Object> values = new JsonSerializer().Deserialize<Dictionary<string, object>>(new JsonTextReader(reader));
return values;
}
Another way that might help explain what was should be done to deserilaize will be -
var jsonString = System.Text.Encoding.Default.GetString(json);
Dictionary<String, Object> values = JsonConvert.DeserializeObject<Dictionary<String, Object>>(jsonString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With