使用Open Weather提供的Swift-2逐行读取JSON文件

时间:2016-08-19 03:21:18

标签: ios json swift ios9 openweathermap

我正在使用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。如果你能告诉我这里的方式,我将非常感激。

2 个答案:

答案 0 :(得分:0)

示例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读取JSON文件并过滤数据,您可以使用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")
}

答案 1 :(得分:0)

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

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

如果需要考虑性能,请尝试按照her e的说明逐行解析文件。迭代文件中的每一行,并对您正在寻找的城市进行简单的旧字符串搜索。这样,您就不会花费大量的计算时间来解析您将不会使用的JSON。