ios - Is there any way to fix a json response -
i parsing json faulty. doing in swift this:
jsonstring.datausingencoding(nsutf8stringencoding, allowlossyconversion: true)
so, gives error.
error converting string object => optional(error domain=nscocoaerrordomain code=3840 "the operation couldn’t completed. (cocoa error 3840.)" (badly formed object around character 11396.) userinfo=0x14512990 {nsdebugdescription=badly formed object around character 11396.})
now, response huge one, posting error part of it:
{ "data" : { "operator_settings" : { "profilesettings" : "{"visible":["firstname","lastname","phonenumber","emailaddress","paddress","paddress2","paddresscity","paddressstateabbreviation","paddresszip","paddresscountry"],"required":["firstname","lastname","emailaddress","paddress","paddresscity","paddresszip"]}" } }, "status" : 200.0, "count" : null }
also, want create response before parsing it:
{ "data": { "operator_settings": { "profilesettings": "{\"visible\":[\"firstname\",\"lastname\",\"phonenumber\",\"emailaddress\",\"paddress\",\"paddress2\",\"paddresscity\",\"paddressstateabbreviation\",\"paddresszip\",\"paddresscountry\"],\"required\":[\"firstname\",\"lastname\",\"emailaddress\",\"paddress\",\"paddresscity\",\"paddresszip\"]}" } }, "status": 200, "count": null
}
but, not know depth of response. not have access api calling. so, there solution can do?
i have requirement yours. same way need pass array values format
"{\n \"userid\" : \"1\",\n \"corporateid\" : \"1\",\n \"dataaccesslevel\" : \"1\"\n}"
so have created 2 methods below:
+ (nsstring *) getjsonstring:(id)object { nsstring *jsonstring = @""; @try { nserror *error = nil; nsdata *jsondata = [nsjsonserialization datawithjsonobject:object options:nsjsonwritingprettyprinted error:&error]; if (! jsondata) { nslog(@"got error: %@", error); } else { jsonstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; } homecoming jsonstring; } @catch (nsexception *exception) { nslog(@" exception :%@",exception); homecoming jsonstring; } } //--------------------------------------------------------------- + (id) getobjectfromjsonstring:(nsstring *)jsonstring { @try { nsdata *jsondata = [jsonstring datausingencoding:nsutf8stringencoding]; nserror *error = nil; id object = [nsjsonserialization jsonobjectwithdata:jsondata options:0 error:&error]; homecoming object; } @catch (nsexception *exception) { nslog(@" exception :%@",exception); homecoming nil; } }
here profilesettings
pass object getjsonstring
method , format.
ios json parsing swift
No comments:
Post a Comment