1

I'm working with a web service and using sbjson to parse received data like this:

NSDictionary *response = [[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]JSONValue];

It works well but failed with a json including binary data (application/pdf) and I got this in the console:

-JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=6 \"Invalid high character in surrogate pair\" UserInfo=0x1e0e1700 {NSLocalizedDescription=Invalid high character in surrogate pair}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=6 \"Broken unicode character\" UserInfo=0x1e0e1740 {NSUnderlyingError=0x1e0e1720 \"Invalid high character in surrogate pair\", NSLocalizedDescription=Broken unicode character}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x1e0e1780 {NSUnderlyingError=0x1e0e1760 \"Broken unicode character\", NSLocalizedDescription=Expected value while parsing array}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: content\" UserInfo=0x1e0e17c0 {NSUnderlyingError=0x1e0e17a0 \"Expected value while parsing array\", NSLocalizedDescription=Object value expected for key: content}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: params\" UserInfo=0x1e0e1800 {NSUnderlyingError=0x1e0e17e0 \"Object value expected for key: content\", NSLocalizedDescription=Object value expected for key: params}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: request\" UserInfo=0x1e0e18d0 {NSUnderlyingError=0x1e0e1890 \"Object value expected for key: params\", NSLocalizedDescription=Object value expected for key: request}"
)

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x3397d2a3 0x3314f97f 0x338ce55f 0x339050d3 0x576c1 0x5c631 0x5d9bf 0x389a7ef5 0x388e79f1 0x388e790d 0x36bf65df 0x36bf5ccb 0x36c1e133 0x338c374d 0x36c1e593 0x36b8215d 0x33952683 0x33951ee9 0x33950cb7 0x338c3ebd 0x338c3d49 0x3462c2eb 0x36509301 0x46ab9 0x38548b20)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

Now I wonder how to solve it without move to another json library. Are there other people else who have ever coped with json including binary? Thanks!

  • NSString *responseStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; Test your json string http://jsonformatter.curiousconcept.com/ – laxonline Dec 31 '12 at 14:08
  • Your problem isn't the JSON library on the client side. It's the invalid JSON data produced by the web service. – Codo Dec 31 '12 at 14:09
  • that troublesome json is from google api... ok. nothing more to say. thank you all. I will report this to google –  Dec 31 '12 at 14:09
  • The Google API has been known to produce invalid JSON from time to time, unfortunately. – Stig Brautaset Jan 02 '13 at 16:48

1 Answers1

1

From: json.org "JSON is a text format". Base64 encode the binary data.

See SO question "Binary Data in JSON String"

Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228