I'm receiving some data from a websocket which seems to be formatted as an Id with a Key Value Pair, which contains a string and some serialized JSON in the format: ID[type,json].
So for example:
42["login",{ "requestId":null,"loginToken":"fwadff","type":"display","clientGroup":null,"features":[],"force":true}]
So in this case: 42 is the Message ID login is the first string in the KVP indicating the type of JSON and the JSON is the second string
So the structure of the JSON-part will change and I need to use the first string to determine how I need to deserialize the JSON.
I could use some string function to isolate the KVP from the Message ID and then convert the KVP, but I can't use Split(',') as it will also split all of the elements from the JSON.
Since this seems like a generic structure, I thought there might be an easier way to do this which I don't probably know of.
What is the easiest way to convert this data into a KVP<string, string> or even KVP[string,dynamic] ?
This is what I have so far:
string test = "42[\"login\",{ \"requestId\":null,\"loginToken\":\"fwadff\",\"type\":\"display\",\"clientGroup\":null,\"features\":[],\"force\":true}]";
// EXTRACT KEY VALUE PAIR FROM STRING
int startIndex = test.IndexOf('[');
int endIndex = test.LastIndexOf(']');
if (startIndex > -1 && endIndex > -1)
{
// EXTRACT KEY VALUE PAIR
string kvp = test.Substring(startIndex, ((endIndex - startIndex)+1));
string id = test.Substring(0, startIndex);
Console.WriteLine(kvp);
var result = kvp.Trim('[', ']')
.Split(new[] { ',' }, 2);
}