1

I'm making a small app to allow users to look up books on openlibrary.org.

The app is still quite basic but works with looking up and parsing the json data. The issue I'm having is creating a check to make sure some data is there in the first place.

On openlibrary, if the ISBN number you search with exists, you get json data: https://openlibrary.org/api/books?bibkeys=0586057242&f&jscmd=data&format=json

If however you look up an ISBN that does not exist, you simply get an empty object returned to you as so: https://openlibrary.org/api/books?bibkeys=123&f&jscmd=data&format=json

How do I implement a check to see if there is actually data there to parse? At the moment my app doesn't crash if there is nothing there, it just doesn't do anything. I'd like to setup an error popup to the user when no data is found, but can not figure out how to do that initial check.

2
  • It would be more friendly if their API gave a 404! Commented Jun 17, 2015 at 23:37
  • Indeed it would, but alas it does not so I have to find a work around. Commented Jun 18, 2015 at 0:08

4 Answers 4

2

What are you expecting? A dictionary? If so, you're in luck. I believe that the empty braces you get when the query results are empty represents an empty dictionary. So just always convert your response to a data object with JSONObjectWithData, then check the count of the result. If the count is zero, your result-set is empty.

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I was expecting to happen but when I do it and it's supposed to fail & give me an error, nothing happens.
Ok, I was being a moron & placing my if statement 2 lines down in the wrong place. This is fixed & works, many thanks for the advice.
1

You only have to check if the result of the JSON parse is nil, in that case the data does not have the correct format.

 NSError *error;
    if ([NSJSONSerialization JSONObjectWithData:JSONdata options:kNilOptionserror:nil] == nil)
    {
        // Handle error
    }

SWIFT:

import Foundation

if NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary == nil{

}

Or you can get a BOOL telling you if the data is correct.

[NSJSONSerialization isValidJSONObject:jsonObj];
//THis actually returns a BOOL

SWIFT:

NSJSONSerialization.isValidJSONObject(jsonObj)

Hope it helps.

2 Comments

Isn't this Obj-C code though & be a pain to integrate into my Swift project?
There you have it in swift, I didnt see the tags
0

Usually, you turn that response into an array. The array can contain arrays of dictionaries, or be an array of dictionaries. Here is how I handle it.

NSError *error;
NSData *URLData = [NSData dataWithContentsOfURL:<yourURL>];
NSArray *array = [NSJSONSerialization JSONObjectWithData:URLData options:NSJSONReadingMutableContainers error:&error];
if (error) {
    NSLog(@"THERE WAS AN ERROR IMPORTING JSON");
}
if ([array count] > 0)
{
    //Something here
} else {
    //NO Data
}

Comments

0

Check if the NSData is equal to NSString "{}"

If let (yourNSDataFromURL as? NSString) == "{}" {/*do what ever you want to**/}

You can also check if the key of certain key is nil or not

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.