Reputation: 232
I have a 256 size buffer which receives data and "stores" them as a byte array. The data contained are JSON Objects, but I can't figure out how to manipulate them. The worst thing I have to deal with is that the JSON objects are stacked. 1st JSON Object contains more than one JSON Objects and every JSON Object contains another one. First byte array contains the first object and the last byte array contains the closing bracket of that first object.
How am I supposed to get the data I want and manipulate them further on? I want to save every object in a SQL database after separating the JSON Objects. Would it be better to implement a list or an array to save the byte arrays or is there a better solution?
Upvotes: 1
Views: 4299
Reputation: 2284
Say the byte array is a string encoded with UTF-8, use Encoding.GetString(byte[] bytes)
to make it a string.
byte[] jsonBytes = GetJsonBytes();
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Now you can use, for example, Newtonsoft.Json to convert it to a DTO type.
MyDto myDtoObj = JsonConvert.DeserializeObject<MyDto>(jsonString);
Upvotes: 1
Reputation: 28654
First convert the byte array to string. This also depends on encoding. Now you should get a normal JSON represented as a string object. You can make sure that your JSON looks "good" now just simply by looking at the string variable and what it contains, which would not have been so easy with the byte array you had in the beginning.
Afterwards, you can try and convert the JSON string from above step to objects.
Upvotes: 1