I have a JSON message in string form which looks something like this:
{"Event":"Payment", "Desc":"Deschereblahblahblah", Data":"{"Result":3,"Reference":12345 ... } }
I just want to know how is best to get the data part of the message, (I'm using JavascriptSerializer currently) and then the inside "Data" into another variable. I can grab the Event easily enough, but if I then try a second Serialization to get the data it errors!
Would it be best to do it dynamically, or to use classes?
If you need to extracy only part of data in one place, I'd recommend you to use JObject
from Newtonsoft Json.Net;
var json =
"{\"Event\":\"Payment\", \"Desc\":\"Deschereblahblahblah\",\"Data\":{\"Result\":3,\"Reference\":12345 } }";
var data = JObject.Parse(json)["Data"];
var result = data["Result"].Value<int>();
var reference = data["Reference"].Value<int>();
You could also prepare special class:
public class Data
{
public int Result { get; set; }
public int Reference { get; set; }
}
Then:
var dataInstance = JObject.Parse(json)["Data"].ToObject<Data>();