2016-08-19 80 views
-1

我正在使用Open Weather API,並且它建議使用cityID進行搜索以獲得最佳和準確的結果。我正在使用CLPlacemark獲取cityName,並且會根據Open Weather提供的JSON文件(「city.list.us.json」)中的cityName進行搜索以獲取cityID。這JSON文件看起來是這樣的:使用Open Weather提供的Swift-2逐行讀取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。如果你能在這裏向我展示方式,我會非常感激。

+0

可能重複[在Swift中逐行讀取文件/ URL](http://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift ) –

+3

這不是有效的JSON。這是10個字典,而JSON文件的根必須是數組或字典。我猜這實際上是10個字典的數組,對嗎? – Alexander

+0

@AlexanderMomchliov號這是一個約20000行的JSON文件,我認爲每行都是一本字典。 –

回答

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}}] 

篩選數據代碼:下面的代碼讀取使用可以過濾使用的ID CITYNAME提供方式相同的JSON文件和過濾數據或其他鍵。

// *** 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") 
} 
+0

恐怕如果按照原樣傳遞JSON,您將無法獲得您期望的數組。你有沒有測試過相同的JSON?我認爲根的JSON數據必須是一個數組或字典。在這裏,我的JSON只是每行的單獨字典。這不是我們的錯,但我認爲API提供商沒有給予足夠的關注。 OpenWeather這樣的着名天氣API如何能夠做到這樣的錯誤?這是另一回事。 –

+0

@VändänÄPatel看看,我已經更新了JSON並製作了字典數組。我假設你也有這樣的數據結構。 –

+0

@VändänÄPatel您正在使用來自API的批量數據,這意味着您已經手動下載文件,然後您需要通過編程或手動方式將其轉換爲Array以實現所需的輸出,否則無法實現。 –

0

這不是有效的JSON。這是10個字典,而JSON文件的根必須是數組或字典。

您可以將整個字符串包裝在[ ... ]中,以將其轉換爲可正常分析的字典數組。

如果性能是一個問題,請嘗試按照說明her e瞭解如何逐行解析文件。遍歷文件中的每一行,併爲您正在尋找的城市做一個普通的舊字符串搜索。這樣,您不會花費噸計算時間解析JSON,您將不會使用。