Question

I am using the SBJSON to convert my NSDictionary to a JSON String in my iOS application.

when my dictionary contains a NSAttributedString or an NSData, the SBJSON fails to generate the string representaiton.

Incase of NSAttributedString, the error is :

-JSONRepresentation failed. Error trace is: ( "Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON serialisation not supported for NSConcreteMutableAttributedString\

Incase of NSData, the error is :

-JSONRepresentation failed. Error trace is: ( "Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON serialisation not supported for NSConcreteMutableData\" UserInfo=0x7ed2560 {NSLocalizedDescription=JSON serialisation not supported for NSConcreteMutableData}"

Solving atleast one of the 2 problems will be a great deal. Please help.

Thanks Roshit

Was it helpful?

Solution

JSON doesn't have any datatype to do what you want, but you could convert the NSData into a Base64 encoded string. This can be done automatically with a category on NSData that implements the -proxyForJson method. The problem is when you need to convert it back to NSData on the other end. If the key is known, then you can just Base64 decode that key. But if the data portion can be for any key it's a bit more difficult. You'll have to somehow structure your data so you can determine which strings should be Base64 decoded.

OTHER TIPS

You can not pass NSData object. Solution for problem is, Just use the following line (change response to your nsdata object) and use that string as a value.

NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

You can not pass NSAttributedString as a value as well. You have to change NSAttributedString to NSString. Please check OHAttributedLabel lib for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top