如何从HttpClient解析JSON字符串? [英] How can I parse JSON string from HttpClient?

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

问题描述

我通过调用外部API得到JSON结果.

I am getting a JSON result by calling an external API.

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync(url).Result;

        if (response.IsSuccessStatusCode)
        {
            var result  = response.Content.ReadAsStringAsync().Result;
            var s = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
            return "Success";
        }
        else
        {
            return "Fail";
        }

我在第var s = Newtonsoft.Json.JsonConvert.DeserializeObject(result);行中得到的结果是:

The result in line var s = Newtonsoft.Json.JsonConvert.DeserializeObject(result); I am getting is like:

 {{
  "query": "1",
  "topScoringIntent": {
    "intent": "1",
    "score": 0.9978111,
    "actions": [
      {
        "triggered": false,
        "name": "1",
        "parameters": [
          {
            "name": "1",
            "required": true,
            "value": null
          },
          {
            "name": "1",
            "required": true,
            "value": null
          },
          {
            "name": "1",
            "required": true,
            "value": null
          }
        ]
      }
    ]
  },
  "entities": [],
  "dialog": {
    "prompt": "1",
    "parameterName": "1",
    "parameterType": "1::1",
    "contextId": "11",
    "status": "1"
  }
}}

我正在使用HttpClient.我在访问prompt键值时遇到困难.我想从dialog获取prompt.我怎么能得到它?

I am using HttpClient. I am facing difficulty in accessing prompt key-value. I want to get prompt from dialog. How can I get it?

推荐答案

您想在这里看看: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

创建具有与XML相同结构的类.然后,变量s是此类的实例,您可以将json反序列化为类结构.

Create a class with the same structure like your XML. Then your variable s is an instance of this class and you can deserialize the json to the class structure.

在您的情况下,您的财产应为s.dialog.prompt.

In your case your property should be s.dialog.prompt.

这篇关于如何从HttpClient解析JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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