【问题标题】:Beginner parsing JSON data? [duplicate]初学者解析 JSON 数据? [复制]
【发布时间】:2013-01-15 20:23:36
【问题描述】:

可能重复:
iPhone/iOS JSON parsing tutorial

我不知道如何解析来自网站的 JSON 数据。

    {
"weather": {
  "curren_weather": [
    {
      "humidity": "54",
      "pressure": "1011",
      "temp": "50",
      "temp_unit": "f",
      "weather_code": "1",
      "weather_text": "Partly cloudy",
      "wind": [
        {
          "dir": "W",
          "speed": "9",
          "wind_unit": "kph"
        }
      ]
    }
  ],

我要做的就是将“temp”和“weather_text”变量存储为 NSStrings 以用于标签或我的应用程序中的任何内容...

实际的请求 url 类似于:http://www.myweather2.com/developer/forecast.ashx?uac=(access-key)&output=json&query=(latitude,longitude)&temp_unit=f

关于如何完成这个有什么想法吗?

【问题讨论】:

  • 你做过研究吗?每种语言都有大量 JSON 库。
  • 您尝试使用什么语言解析 JSON? Javascript / C# 等...
  • 我在这里发帖之前总是先做研究,我尝试按照 countlss 教程进行操作,但无济于事,我不确定自己做错了什么。这就是我来这里的原因。
  • 目标 C,iOS 开发
  • 在 Google 上快速搜索“iOS JSON 解析”会产生大量点击。没有任何满足您的需求吗?

标签: json


【解决方案1】:

我希望这不是全部 - 最后应该有几个额外的 }s,否则它不是有效的 JSON。

使用NSJSONSerializationJSONObjectWithData:options:error: 方法解析JSON。对于上面的 JSON,它会给你一个NSDictionary。然后,您可以使用NSDictionary 方法或键值编码来查找值。如果您修复 JSON 并以这种方式解析它,您将使用像 weather.curren_weather.temp 这样的关键路径(真的是 curren_weather 而不是 current_weather?)来获取温度。 p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2022-11-21
    • 2015-02-11
    • 2019-05-30
    相关资源
    最近更新 更多