簡體   English   中英

c#反序列化Json字符串

[英]c# Deserialize Json String

我是 REST 的新手(從 SOAP 開始)。 我正在嘗試反序列化作為 REST API 調用的結果的 JSON 字符串。

這是 json 字符串:

{
  "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
}

我在 rtime 字段的值中需要什么。

這是我的代碼:

var json_serializer = new JavaScriptSerializer();
        var routes_list = (IDictionary<string, object>)json_serializer.DeserializeObject(AMI_GetResult.GetResult(data));    
        return string.Concat(routes_list["rtime"]);

我收到此錯誤:

System.Collections.Generic.KeyNotFoundException

我知道在解析中沒有找到 rtime,我的問題是:

如何獲取 rtime 的時間戳值?

謝謝你的幫助。

Result 屬性不包含另一個 json 對象它包含 json 字符串,您需要再次反序列化它。 例如使用 Json.NET,它看起來像這樣:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM