-1

I am working with Open Weather API, and it suggests to search with cityID for the best and accurate results. I am getting cityName using CLPlacemark, and I would search against that cityName in JSON file ("city.list.us.json") provided by Open Weather to get the cityID. That JSON file looks something like this :

{"_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}}

I have seen numerous examples where you would read entire file, but here I would have to read line by line and check it against my cityName to get the cityID. I would really appreciate it if you can show me the way here.

Alexander
  • 59,041
  • 12
  • 98
  • 151
Vandan Patel
  • 1,012
  • 1
  • 12
  • 23
  • Possible duplicate of [Read a file/URL line-by-line in Swift](http://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift) – Ishmeet Aug 19 '16 at 03:51
  • 3
    That's not valid JSON. That's 10 Dictionaries, whereas the root of a JSON file must be either an Array or a Dictionary. I presume this is actually an Array of 10 Dictionaries, correct? – Alexander Aug 19 '16 at 04:04
  • @AlexanderMomchliov No. It's a JSON file with around 20000 lines, and I think each line is a dictionary. – Vandan Patel Aug 19 '16 at 04:23
  • 1
    Try the answer linked in that other question. Iterate over each line in the file, and do a plain old string search for the city you're looking for. That way, you don't spend tons of compute time parsing JSON you won't be using. – Alexander Aug 19 '16 at 04:33
  • @VändänÄPatel what i understand is you need a json object matching your city, right? – Dipen Panchasara Aug 19 '16 at 05:37
  • @DipenPanchasara That's right. I have my cityName provided by device's GPS, and I would iterate through that JSON file to get the corresponding cityID. In order to do that, I have to compare my cityName with the cityNames in JSON. To access cityNames in JSON, I need access to each line. I hope I am not confusing you. – Vandan Patel Aug 19 '16 at 05:44
  • 2
    @AlexanderMomchliov I think you are right. This is not a valid JSON. Shame of Open Weather. If I make them an array of comma separated dictionaries, it works like a charm. So I learnt something very important today. The root of a JSON file must be either Array of Dictionary. – Vandan Patel Aug 19 '16 at 06:03
  • @VändänÄPatel I posted my comments as an answer so that this question can be closed – Alexander Aug 19 '16 at 15:30

2 Answers2

0

Sample JSON Array : I assume you have an Array of Dictionary

[{"_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}}]

Filter Data code : Following code reads JSON file and filters data using CityName provided, same way you can filter using id or other key.

// *** 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")
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • I am afraid that if you pass the JSON as it is, you won't get an array you are hoping for. Have you tested it for the same JSON by any chance? I think root the the JSON data has to be an array or dictionary. Here my JSON is just separate dictionaries on each line. It's not our fault, but the API provider has not paid enough attention I guess. How can famous weather API like OpenWeather can do such mistake? That's another thing to think about. – Vandan Patel Aug 19 '16 at 06:52
  • @VändänÄPatel take a look, i have updated JSON and made array of dictionary. I assume you too have such data structure. – Dipen Panchasara Aug 19 '16 at 07:04
  • @VändänÄPatel you are using bulk data from API right, which means you have downloaded file manually, then you need to convert it to Array programatically or manually to achieve desired output, otherwise its not possible. – Dipen Panchasara Aug 19 '16 at 07:09
  • I am sorry, I didn't have a closer look at your JSON. You are absolutely right in making it an array, but my API provider doesn't provide an array. They provide JSON just like I provided here - separate set of dictionaries on each line. – Vandan Patel Aug 19 '16 at 07:10
  • Thats what i have written in my previous comment, kindly read my previous comment for it. – Dipen Panchasara Aug 19 '16 at 07:13
  • You are right, but I was just trying to refer to the problem I was facing. If I had the array of dictionaries, I wouldn't have any trouble. – Vandan Patel Aug 19 '16 at 08:02
0

That's not valid JSON. That's 10 Dictionaries, whereas the root of a JSON file must be either an Array or a Dictionary.

You can wrap the whole string in [ ... ] to convert it into an Array of Dictionaries, which you can parse normally.

If performance is a concern, try following the instructions here for how to parse a file line by line. Iterate over each line in the file, and do a plain old string search for the city you're looking for. That way, you don't spend tons of compute time parsing JSON you won't be using.

Community
  • 1
  • 1
Alexander
  • 59,041
  • 12
  • 98
  • 151