Reputation:
I am trying to parse a json to access the child value. Below is the sample json,
jsondata = {
"sample_data": "{"o2:{"testname":"o2","department":"chemistry","normalvalue":"l","testmethod":"j","specimen":"g","referelprice":"y","normalprice":"i","discountprice":"o"}}"
}
Below is my code
var _json = JObject.Parse(jsondata.ToString());
Console.WriteLine(_json["sample_data"]);
This code gives me the output
{"o2":{"testname":"o2","department":"chemistry","normalvalue":"l","testmethod":"j","specimen":"g","referelprice":"y","normalprice":"i","discountprice":"o"}}
Now if i try to get the Child "o2" by using
Console.WriteLine(_json["sample_data"]["o2"]);
i am getting below error message
cannot access child value on newtonsoft.json.linq.jvalue
Please help me on how to get the child value. I want the output like
{"testname":"o2","department":"chemistry","normalvalue":"l","testmethod":"j","specimen":"g","referelprice":"y","normalprice":"i","discountprice":"o"}
How can i achieve this. Please help.
Upvotes: 3
Views: 7283
Reputation: 18127
You can do it using dynamic.
dynamic a = JsonConvert.DeserializeObject(yourJson);
Console.WriteLine(a.sample_data.o2.ToString());
Upvotes: 1
Reputation: 3689
As in your sample jsondata
value stored againt sample_data
is string.
Try this
var _json = JObject.Parse(jsondata.ToString());
var sampledataJson = JObject.Parse(_json["sample_data"].ToString());
Console.WriteLine(sampledataJson["o2"]);
Upvotes: 4