How to retrieve value from Json string in C#

本小妞迷上赌 提交于 2021-02-05 06:37:05

问题


I'm getting a response like

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

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


回答1:


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


来源:https://stackoverflow.com/questions/37451580/how-to-retrieve-value-from-json-string-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!