Validate an input as JSON notation on iOS -


i have input field on ios app accepts nsstring value. want able validate input json object. example:

nsstring = @"{'foo':'bar'}" //would validated json notation nsstring = @"hello world!" //would not validated json 

i have tried using following method:

[nsjsonserialization isvalidjsonobject:(id)obj] 

however, returns false, if string input {'hello':'world'}. there i'm doing wrong or missing here?

isvalidjsonobject not way see if string represents valid json. it's meant see if nsarray or nsdictionary can converted json string.

to see if json string valid json, use normal json serialization (jsonobjectwithdata:options:error:) , see if valid result.

nsstring *jsonstring = ... // json wish validate nserror *error = nil; id result = [nsjsonserialization jsonobjectwithdata:[jsonstring datausingencoding:nsutf8encoding] options:0 error:&error]; if (result) {     // string valid json } else {     // string not valid json     // log error see wrong } 

Comments