如何从C#中的Json字符串中检索值 [英] How to retrieve value from Json string in C#

查看:76
本文介绍了如何从C#中的Json字符串中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到类似

{ "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"值.

I need the value of "copy_ref" from this response string in C#.

推荐答案

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

Here's a small console application showing how you'd retrieve it using Json.NET. In your case the string, "json" would be retrieved from the response.

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);
}

这篇关于如何从C#中的Json字符串中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆