0

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.

1
  • Can you edit your question to format your JSON? I tried editing it, but all the encoding overwhelmed my hand-editing skills. Since it's so encoded, I can just run it through a "format this JSON" tool I wrote. Where did you get this from, the debugger? Instead of doing it that way, use the "Text Visualizer" in the debugger. At a breakpoint, over over your JSON string, One of the things you should see is a small pull-down widget with a magnifying class. Pull it down, click "Text Visualizer". Copy/paste that string and format it as code Commented Jun 17, 2021 at 23:08

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.