[英]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.