LoginSignup
6
6

More than 5 years have passed since last update.

【iOS】NSDataがJSONかどうか判定する方法

Last updated at Posted at 2015-03-29

NSDataがJSONかどうか判定する方法

とあるAPIを叩いた際に、JSONか画像のどちらかが返ってくるという状態で、NSDataがJSONかどうかを判定する必要があり、if([NSJSONSerialization isValidJSONObject:data]){ としてもうまくいきませんでしたが、JSONObjectWithDataを利用することで判定できました。

isjson.m
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
                                          options:NSJSONReadingMutableContainers
                                          error:nil];
if(json != nil) {
     // json
}else{
     // not json
}

参考:StackOverflow
http://stackoverflow.com/questions/17149868/isvalidjsonobject-not-working-as-expected

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6