Reputation: 23
I'm new to REST (moving from SOAP). I'm trying to deserialize a JSON string i'm getting as result to a REST API call.
This is the json string:
{
"message": "SUCCESS",
"result": "{\"code\":\"610a187f80e24542bd5342d93c810a04\",\"rtime\":\"2021-06-17 19:40:03\",\"data\":\"08000002=2021-06-17 19:42:06\",\"ref_code\":\"\",\"stime\":\"2021-06-17 19:40:03\",\"source\":\"inhe-iot-crw_192_168_0_201_7021\",\"cycle\":0,\"operator\":\"ADMIN\",\"head\":\"inhe-ami-center/51ad33de59bb44dda693ec9811cec826\",\"result\":\"S\",\"times\":0,\"dev\":{\"ptct\":\"1/1|1/1\",\"svn_type\":\"791f88ae4392a682eb74\",\"con\":\"\",\"scheme\":\"DLMS_WRAPPER_645\",\"svn_no\":12,\"id\":\"00036178fedf95aa8b68\",\"addr\":\"037222663645\"},\"op_content\":\"037222663645||0\",\"ptime\":\"2021-06-17 19:40:03\",\"op_times\":\"0\",\"module_type\":\"001\",\"ip\":\"\",\"params\":\"\",\"priority\":9,\"packet\":\"S:0001000100010018E6000508AAA4C5DD68453666227203681104D3343337CE16,R:FEFEFEFE6845366622720368910BD334333733395D464439482916\",\"cmd_type\":\"R-READ\",\"field\":\"08000002\",\"expire\":10,\"etime\":\"2021-06-17 19:40:04\",\"data_ptct\":\"08000002=2021-06-17 19:42:06\"}",
"code": 0,
"sign": "d488e1e531837929faeac2518c1659e6",
"timestamp": 1623969605496
}
What i need in the value for rtime field.
This is my code:
var json_serializer = new JavaScriptSerializer();
var routes_list = (IDictionary<string, object>)json_serializer.DeserializeObject(AMI_GetResult.GetResult(data));
return string.Concat(routes_list["rtime"]);
I'm receiving this error:
System.Collections.Generic.KeyNotFoundException
I understand that rtime is not being found in the parse, my question is:
How can i get the timestamp value for rtime?
Thanks for your help.
Upvotes: 0
Views: 158
Reputation: 143473
Result property does not contain another json object it contains json string, you need to deserialize it again. For example using Json.NET it can look something like that:
public class Outer
{
public string Result { get; set; }
}
public class Inner
{
public string RTime { get; set; } // or even make it of type DateTime, but there can be some format related issues
}
var outer = JsonConvert.DeserializeObject<Outer>(json);
var inner = JsonConvert.DeserializeObject<Inner>(outer.Result); // RTime prop will contain needed data
Upvotes: 1