【问题标题】:How to read JSON response in c#如何在 C# 中读取 JSON 响应
【发布时间】:2012-03-29 13:43:45
【问题描述】:

我正在使用第三方服务来给我坐标,下面是我想在某种对象中使用 c# .net 阅读此内容的响应,以便我可以使用这些信息但对如何实现这一点感到困惑..

{"found": 1, "bounds": [[52.45401, -1.96211], [52.45401, -1.96211]], "features": [{"id": 65140,"centroid": {"type":"POINT","coordinates":[52.45401, -1.96211]},"bounds": [[52.45401, -1.96211], [52.45401, -1.96211]],"properties": {"name": "B17 0SL"},"type": "Feature"}], "type": "FeatureCollection", "crs": {"type": "EPSG", "properties": {"code": 4326, "coordinate_order": [0, 1]}}}

谢谢

【问题讨论】:

标签: c# .net json


【解决方案1】:

看看 Newtonsoft.Json,它是一个包,可以将 Json 反序列化为一个类。

但是你需要创建你想要使用的类结构。

【讨论】:

  • 如何创建一个类结构,我的意思是我不知道这是如何组织的,我对此完全陌生。
  • @Shax 我在 Codeplex jsonclassgenerator.codeplex.com 上找到了这个项目,它是一个应用程序,它将获取字符串并为您提供可以反序列化的类结构
【解决方案2】:

使用 json 解析器,如 DataContractJsonSerializerJavaScriptSerializer

对于你的情况,例如,使用Json.Net & dynamic 关键字,你可以写

dynamic jObj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine(jObj.found);
Console.WriteLine(jObj.features[0].bounds[0][0]);

【讨论】:

    【解决方案3】:

    您可以使用JsonTextReader。如果您不使用 JSON.NET,以下代码段可能很有用

    jsonString = {"found": 1, "bounds": [[52.45401, -1.96211], [52.45401, -1.96211]], "features": [{"id": 65140,"centroid": {"type":"POINT","coordinates":[52.45401, -1.96211]},"bounds": [[52.45401, -1.96211], [52.45401, -1.96211]],"properties": {"name": "B17 0SL"},"type": "Feature"}], "type": "FeatureCollection", "crs": {"type": "EPSG", "properties": {"code": 4326, "coordinate_order": [0, 1]}}};
    JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));
    
    while (reader.Read())
    {
       if (reader.Value != null)
       {
          // Read Json values here
          // reader.Path               -> Gives you the key part.
          // reader.Value.ToString()   -> Gives you the value part
       }
    }
    

    【讨论】:

      【解决方案4】:

      您可以像这样读取 JSON:

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");
      
                  JArray array = new JArray();
                  using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
                  {
      
                      using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
                      {
                          JavaScriptSerializer js = new JavaScriptSerializer();
                          var objText = reader.ReadToEnd();
      
                          JObject joResponse = JObject.Parse(objText);
                          JObject result = (JObject)joResponse["result"];
                          array = (JArray)result["Detail"];
                          string statu = array[0]["dlrStat"].ToString();
                      }
      
                  }
      

      【讨论】:

        【解决方案5】:

        您可以使用JSON.NET

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-12
          • 2016-02-29
          • 2014-04-15
          相关资源
          最近更新 更多