如何解析来自Httpclient的json字符串 [英] How can I parse json string from Httpclient

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

问题描述

我通过调用外部api得到json结果.

Hi i am getting 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"
  }
}}

如何获取对话"的prompt? 我正在使用http客户端.我在访问提示键值时遇到困难. 我想从对话框中获得提示.我怎么能得到它?

how can I get prompt of 'dialog'? I am using http client. 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天全站免登陆