繁体   English   中英

如何在C#中从Json字符串检索值

[英]How to retrieve value from Json string in C#

我得到像这样的回应

{ "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZbnmbnm", "Result": null, "error": null }
    base: { "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZ0aWRibWw2aA", "Result": null, "error": null }
    ContentDisposition: null
    ContentType: "application/json"
    HttpHeaders: {Connection: keep-alive
expires=Tue, 25 May 2021 04:10:58 GMT; 
}
    IsArray: true
    IsSuccessfully: true
    IsXml: true
    Result: { "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZbnmbnm", "Result": null, "error": null }
    StatusCode: 200

我需要C#中此响应字符串的“ copy_ref”值。

这是一个小型控制台应用程序,显示了如何使用Json.NET检索它。 在您的情况下,将从响应中检索字符串“ json”。

static void Main()
{
    string json = @"
        { 'expires': 'Sat, 19 May 2046 04:10:58 + 0000', 'copy_ref': 'SMJNA2wxbGZbnmbnm', 'Result': null, 'error': null }";

    JObject jObj = JObject.Parse(json);                 // Parse the object graph
    string copyRef = jObj["copy_ref"].ToString();       // Retrive value by key

    Console.WriteLine(copyRef);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM