【问题标题】:Reading a JSON file line by line using Swift-2 provided by Open Weather使用 Open Weather 提供的 Swift-2 逐行读取 JSON 文件
【发布时间】:2016-08-19 03:21:17
【问题描述】:

我正在使用 Open Weather API,它建议使用 cityID 搜索以获得最佳和准确的结果。我正在使用 CLPlacemark 获取 cityName,我将在 Open Weather 提供的 JSON 文件(“city.list.us.json”)中搜索该 cityName 以获取 cityID。该 JSON 文件看起来像这样:

{"_id":4070245,"name":"Jones Crossroads","country":"US","coord":{"lon":-85.484657,"lat":31.21073}}
{"_id":4344544,"name":"Vernon Parish","country":"US","coord":{"lon":-93.183502,"lat":31.11685}}
{"_id":4215307,"name":"Pennick","country":"US","coord":{"lon":-81.55899,"lat":31.313}}
{"_id":5285039,"name":"Black Bear Spring","country":"US","coord":{"lon":-110.288139,"lat":31.386209}}
{"_id":4673179,"name":"Bee House","country":"US","coord":{"lon":-98.081139,"lat":31.40266}}
{"_id":4047656,"name":"Provo","country":"US","coord":{"lon":-94.107697,"lat":34.037609}}
{"_id":5493998,"name":"Tejon","country":"US","coord":{"lon":-105.28611,"lat":34.58979}}
{"_id":5815135,"name":"Washington","country":"US","coord":{"lon":-120.501472,"lat":47.500118}}
{"_id":5391891,"name":"San Dimas","country":"US","coord":{"lon":-117.806732,"lat":34.106682}}
{"_id":4056099,"name":"Coffee County","country":"US","coord":{"lon":-86.000221,"lat":31.41683}}

我见过很多例子,你会阅读整个文件,但在这里我必须逐行阅读,并根据我的 cityName 检查它以获取 cityID。如果您能告诉我这里的路,我将不胜感激。

【问题讨论】:

  • 这不是有效的 JSON。那是 10 个字典,而 JSON 文件的根必须是数组或字典。我想这实际上是一个包含 10 个字典的数组,对吗?
  • @AlexanderMomchliov 不。这是一个大约 20000 行的 JSON 文件,我认为每一行都是一个字典。
  • 尝试其他问题中链接的答案。遍历文件中的每一行,然后对您要查找的城市进行普通的旧字符串搜索。这样,您就不会花费大量计算时间来解析您不会使用的 JSON。
  • @AlexanderMomchliov 我认为你是对的。这不是有效的 JSON。开放天气的耻辱。如果我让它们成为逗号分隔的字典数组,它就像一个魅力。所以我今天学到了一些非常重要的东西。 JSON 文件的根必须是字典数组。

标签: ios json swift ios9 openweathermap


【解决方案1】:

示例 JSON 数组: 我假设您有一个字典数组

[{"_id":4070245,"name":"Jones Crossroads","country":"US","coord":{"lon":-85.484657,"lat":31.21073}},
{"_id":4344544,"name":"Vernon Parish","country":"US","coord":{"lon":-93.183502,"lat":31.11685}},
{"_id":4215307,"name":"Pennick","country":"US","coord":{"lon":-81.55899,"lat":31.313}},
{"_id":5285039,"name":"Black Bear Spring","country":"US","coord":{"lon":-110.288139,"lat":31.386209}},
{"_id":4673179,"name":"Bee House","country":"US","coord":{"lon":-98.081139,"lat":31.40266}},
{"_id":4047656,"name":"Provo","country":"US","coord":{"lon":-94.107697,"lat":34.037609}},
{"_id":5493998,"name":"Tejon","country":"US","coord":{"lon":-105.28611,"lat":34.58979}},
{"_id":5815135,"name":"Washington","country":"US","coord":{"lon":-120.501472,"lat":47.500118}},
{"_id":5391891,"name":"San Dimas","country":"US","coord":{"lon":-117.806732,"lat":34.106682}},
{"_id":4056099,"name":"Coffee County","country":"US","coord":{"lon":-86.000221,"lat":31.41683}}]

过滤数据代码:以下代码读取 JSON 文件并使用提供的 CityName 过滤数据,与使用 id 或其他键过滤的方式相同。

// *** Read JSON file ***
let path = NSBundle.mainBundle().pathForResource("weather", ofType: "json")
let data = NSData(contentsOfFile: path!)

// *** Declare array ***
var arr:[AnyObject]

// *** Apply filter on array with filter string ***
let filterCityName = "San Dimas"
do {
    arr = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! [AnyObject]
    let filteredObject = arr.filter({
        $0["name"] as! String == filterCityName //access the value to filter
    })
    // *** Print result ***
    print(filteredObject)
}
catch{
    print("Exception")
}

【讨论】:

  • 恐怕如果您按原样传递 JSON,您将无法获得您希望的数组。您是否有机会为相同的 JSON 测试过它?我认为 JSON 数据必须是数组或字典。在这里,我的 JSON 只是每一行上的单独字典。这不是我们的错,但我猜 API 提供者没有给予足够的重视。像 OpenWeather 这样著名的天气 API 怎么会犯这样的错误呢?这是另一件需要考虑的事情。
  • @VändänÄPatel 看看,我已经更新了 JSON 并制作了字典数组。我假设你也有这样的数据结构。
  • @VändänÄPatel 您正在使用来自 API 的批量数据,这意味着您已经手动下载了文件,然后您需要以编程方式或手动将其转换为 Array 以获得所需的输出,否则不可能。跨度>
  • 很抱歉,我没有仔细查看您的 JSON。你把它变成一个数组是绝对正确的,但是我的 API 提供者没有提供一个数组。他们提供 JSON 就像我在此处提供的一样 - 每行都有一组单独的字典。
  • 这就是我在之前的评论中写的,请阅读我之前的评论。
【解决方案2】:

这不是有效的 JSON。那是 10 个字典,而 JSON 文件的根必须是数组或字典。

您可以将整个字符串包装在[ ... ] 中,将其转换为字典数组,您可以正常解析。

如果性能是一个问题,请尝试按照说明here 了解如何逐行解析文件。遍历文件中的每一行,然后对您要查找的城市进行普通的旧字符串搜索。这样,您就不会花费大量计算时间来解析您不会使用的 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-04
    • 2014-08-26
    • 2015-10-25
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多