c #json拉请求

时间:2017-03-07 08:44:39

标签: c# json

我的JSON如下:

json image

 string url = "https://kgsearch.googleapis.com/v1/entities:search?languages=tr&query=Necati+Şaşmaz&limit=1&key=AIzaSyB_w5S8Bv3P4zn7pqu6if49_4bHx9YMmSY";
 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 string jsonVerisi = "";
 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
 {
      StreamReader reader = new StreamReader(response.GetResponseStream());
      //jsonVerisi adlı değişkene elde ettiği veriyi atıyoruz.
      jsonVerisi = reader.ReadToEnd();
 }

如何使用json获取所选图像的位置?

1 个答案:

答案 0 :(得分:0)

有两种方法可以实现这个目标

  1. 如果返回的JSON内容架构(结构)始终相同,请尝试创建一个类,如下所示
  2. 注意:这些类是使用json2csharp

    生成的
        public class Context {
        [JsonProperty("@vocab")] //This is to map the property name with @symbol in JSON content to this class property
         public string vocab { get; set; }
         public string goog { get; set; }
         public string EntitySearchResult { get; set; }
         public string detailedDescription { get; set; }
         public string resultScore { get; set; }
         public string kg { get; set; }
        }
    
        public class DetailedDescription {
         public string articleBody { get; set; }
         public string url { get; set; }
         public string license { get; set; }
        }
    
        public class Result {
         [JsonProperty("@id")]
         public string id { get; set; }
         public string name { get; set; }
         [JsonProperty("@type")]
         public List<string> type { get; set; }
         public string description { get; set; }
         public DetailedDescription detailedDescription { get; set; }
        }
    
        public class ItemListElement {
         [JsonProperty("@type")]
         public string type { get; set; }
         public Result result { get; set; }
         public double resultScore { get; set; }
        }
    
        public class SearchResult {
         [JsonProperty("@context")]
         public Context context { get; set; }
         [JsonProperty("@type")]
         public string type { get; set; }
         public List<ItemListElement> itemListElement { get; set; }
        }
    

    然后尝试使用JsonConvert from Newtonsoft Json.NET反序列化jsonVerisi值,如下所示

        using(HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
                using(StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    //jsonVerisi adlı değişkene elde ettiği veriyi atıyoruz.
                    jsonVerisi = reader.ReadToEnd();
    
                    //Deserialize
                    var searchResult = JsonConvert.DeserializeObject<SearchResult>(jsonVerisi);
                    //Now you could get the needed value as below
                    if(searchResult != null) {
                        foreach(var item in searchResult.itemListElement) {
                            var resultTypes = item.result != null ? item.result.type : null;
    
                            //Iterate through resultTypes to get the list of values
                            //e.g. for person
                            //var personType = resultTypes[1];
                        }
                    }
                }
            }
    
    1. 使用类型Newtonsoft Json.NET JObject(提供类似于Dictionary<string, object>JSON key or property name的元素访问)而不是创建新类,并反序列化JSON内容,如下所示。 / p>

      using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)     {
              using(StreamReader reader = new StreamReader(response.GetResponseStream())) {
                  //jsonVerisi adlı değişkene elde ettiği veriyi atıyoruz.
                  jsonVerisi = reader.ReadToEnd();
      
                  var searchResult = JsonConvert.DeserializeObject<JObject>(jsonVerisi);
                  if(searchResult != null) {
                      var itemListElement = searchResult["itemListElement"];
                      if(itemListElement != null) {
                          foreach(var item in itemListElement.ToList()) {
                              var result = item["result"];
                              var resultTypes = result != null && result["@type"].HasValues ? result["@type"].Values<string>().ToList() : null;
      
                              //Iterate through resultTypes to get the list of values
                              //e.g. for person
                              //var personType = resultTypes[1];
                          }
                      }
                  }
              }
          }
      
    2. 我希望这会对你的情况有所帮助!

      提示:最好使用HttpClient类型,它有许多有用的方法来执行HTTP请求。