简体   繁体   English

如何从httpClient.PostAsJsonAsync绑定JSON响应

[英]How to bind JSON response from httpClient.PostAsJsonAsync

I can httpClient.PostAsJsonAsync(path, content) fine. 我可以httpClient.PostAsJsonAsync(路径,内容)罚款。

However, this post returns some JSON with details of the response, eg: {"StatusCode":200,"AccessCode":"92BEEB285ZB47DA","InternalMessage":null} 但是,这篇文章返回了一些带有响应细节的JSON,例如:{“StatusCode”:200,“AccessCode”:“92BEEB285ZB47DA”,“InternalMessage”:null}

I need to access the AccessCode. 我需要访问AccessCode。

How can I do this cleanly and efficiently? 如何干净有效地完成这项工作? Can I create an object like this: 我可以创建这样的对象:

public class GIResponse
{
    public string StatusCode { get; set; }
    public string AccessCode { get; set; }
    public string InternalMessage { get; set; }
}

And map it to the result? 并将其映射到结果?

Or how would I just traverse the JSON and pull out the AccessCode? 或者我将如何遍历JSON并拔出AccessCode?

I have searched quite extensively but surprisingly I can't find anything on Google - perhaps as this is the result from a Post, not a Get. 我搜索得非常广泛,但令人惊讶的是我在Google上找不到任何东西 - 也许这是因为这是Post的结果,而不是Get。

How can I do this? 我怎样才能做到这一点?

Provided that you get the responseText using httpResponse.Content.ReadAsStringAsync , you can use Json.NET's JObject and define it as dynamic: 如果使用httpResponse.Content.ReadAsStringAsync获取responseText ,则可以使用Json.NET的JObject并将其定义为动态:

dynamic j = JObject.Parse(@"{""StatusCode"":200,""AccessCode"":""92BEEB285ZB47DA"",""InternalMessage"":null}");
Console.WriteLine(j.AccessCode);

Also you can use JsonConvert : 你也可以使用JsonConvert

var result = JsonConvert.Deserialize<MyModel>(resposeText);

Obviously, if you already have a model, you do not read it as a string and you can simply read it as your model: 显然,如果您已经有一个模型,则不要将其作为字符串读取,您只需将其作为模型阅读:

var result = httpResponse.Content.ReadAsAsync<MyModel>();

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

相关问题 HttpClient.PostAsJsonAsync 无法从 ASPX 工作的问题 - Issue with HttpClient.PostAsJsonAsync Not Working from ASPX HttpClient.PostAsJsonAsync引发TaskCanceledException - HttpClient.PostAsJsonAsync throws TaskCanceledException HttpClient.PostAsJsonAsync 无一例外地崩溃了 - HttpClient.PostAsJsonAsync crushed without exception HttpClient.PostAsJsonAsync崩溃而不抛出异常 - HttpClient.PostAsJsonAsync crashing without throwing exception 使用 HttpClient.PostAsJsonAsync(something) 时是否可以使用 Newtonsoft.Json? - Is it possible to use Newtonsoft.Json when using HttpClient.PostAsJsonAsync(something)? 如何确定HttpClient.PostAsJsonAsync调用上500错误的问题是什么? - How do I determine what is the issue with a 500 error on a HttpClient.PostAsJsonAsync call? HttpClient.PostAsJsonAsync与MVC控制器动作之间的通信 - Communication between HttpClient.PostAsJsonAsync and MVC controller actions 通过HTTPClient.PostAsJsonAsync调用的JsonConverter中的HttpContext不可用 - HttpContext unavailable in JsonConverter called via HTTPClient.PostAsJsonAsync HttpClient.PostAsJsonAsync永远不会看到帖子成功和响应的时间 - HttpClient.PostAsJsonAsync never sees when the post is succeeding and responding C# HttpClient.PostAsJsonAsync 因内部服务器错误而失败 - C# HttpClient.PostAsJsonAsync Failing with Internal Server Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM