我正在使用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。如果你能在這裏向我展示方式,我會非常感激。
可能重複[在Swift中逐行讀取文件/ URL](http://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift ) –
這不是有效的JSON。這是10個字典,而JSON文件的根必須是數組或字典。我猜這實際上是10個字典的數組,對嗎? – Alexander
@AlexanderMomchliov號這是一個約20000行的JSON文件,我認爲每行都是一本字典。 –