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
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.
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.
Related
Is there a way I can NSLog JSON response of NSData in JSONformat?
NSLog(#"JSON NSString: %#" ,jsonData);
In this post they are printing NSDictionary,I can convert it to NSDictionary. and this solution returns (null).
How can I NSLog in JSON format?
• What's wrong:
jsonData (as you gave) IS NOT a hexData representing a JSON.
• Quick hack (not viable solution!) to get your JSON to use in your site CodeBeautify:
NSDictionary *dictFromData = [NSKeyedUnarchiver unarchiveObjectWithData:jsonData];
NSData *realJSONData = [NSJSONSerialization dataWithJSONObject:dictFromData options:0 error:nil];
NSString *strFINAL = [[NSString alloc] initWithData:realJSONData encoding:NSUTF8StringEncoding];
NSLog(#"StrFINAL: %#", strFINAL);
Note: Yeah, I bypassed the error parameters, and we shouldn't. With
NSJSONWritingPrettyPrinted instead of 0 in options: parameter, you have a result almost similar to the one of CodeBeautify.
• How did I get there:
Firt, I copy/paste your bump string of NSData with this answer.
That way, I got jsonData as you got.
Then, I tried simply what it should be given your informations:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&errorJSON];
Which didn't work giving the error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be
completed. (Cocoa error 3840.)" (Invalid value around character 0.)
UserInfo=0x17598540 {NSDebugDescription=Invalid value around character
0.}
But with NSDictionary *dictWithData = [NSKeyedUnarchiver unarchiveObjectWithData:jsonData];, I managed to get the real NSDictionary. But NSKeyedArchiver/NSKeyedUnarchiver are doing something "equivalent" to NSJSONSerialization: it serializes, transforming a NSObject into NSData (and vice-versa). But more powerful: for any kind of object that are NSCoding compliant. Here, since it's originally from a JSON (only NSString, NSNumber, NSArray and NSDictionary objects, and not a custom one), it's working without any more code.
Did you for instance tried to save it into NSUserDefaults and it's not a .plist either (that was also one on my tries, I saved jsonData into memory, and used dictionaryWithContentsOfFile: giving me weird answer, but a important one in the bump of it: ""$class" = "{value = 23}";" which lead me to NSKeyArchiver/NSKeyUnarchiver). I don't know what you did exactly.
• Conclusion:
So clearly, somewhere, you mixed stuff found on the web. You need to rework that. You can't let it like this. There is issue in your code elsewhere. Where did you get jsonData from? What did you do with it?
Code:
NSLog("Formatted JSON : %#" ,[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
There are different situations. If parsing the JSON data was fine, then you just want to log the result (dictionary or array). If parsing the JSON data failed, and you suspect there is something wrong with the JSON data, then you convert the JSON data to an NSString and log that. And finally, if either conversion to NSString failed, or you look at the NSString and can't find what's wrong with it, then you log the NSData itself to be able to see the bytes. That's useful if someone managed to put control characters or some other nonsense into your JSON data.
The best is to write a method (warning! not for the timid! requires writing code yourself) that takes the NSData, analyses it and prints out the information that you need.
I want to store my Rich Text for UITextView's NSAttributedString. For this as suggested in a question on stackoverflow, I choose NSData. Problem is app crashes while un-archiving and retrieved data is also not same as the saved NSData.
Explanation
DB:
Saving Data into DB
In DB I have a column named rtfText with datatype blob -> (rtfText blob)
While Saving into DB, I do this
NSData *data = [NSKeyedArchiver archiveWithRootObject:_myTextView.attributedString];
and sends the data like this in query
NSString Query = [NSString stringWithFormat:#"Insert into table (rtfText) VALUES \"%#\")",data];
Retrieving Data From DB
Data is retrieved like this:
NSData *data = [[NSData alloc] initWithBytes:(sqlite3_column_blob(statement, 18)) length:sqlite3_column_bytes(statement, 18)];
UnArchive:
From the retrieved NSData from DB I unarchive it into NSAttributedString like this
_myTextView.attributedText = [NSKeyedUnarchiver unarchiveObjectWithData:data]; **<- App Crashes at this point giving error**
Error:
-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x15d56e80
I even tried to save NSAttributedString to NSData after Archive then converting it to NSString using NSASCIIStringEncoding but DB crashes then. I also tried saving it like this. First using NSKeyedArchiver converted NSAttributedString to NSData then to NSString using NSUTF8StringEncoding which gave me null string.
Kindly look into this.
Thanks in advance
Data Never inserted properly into database. Reason is Query on sqlite3 is always in the form of UTF8. NSString has to be converted to UTF-8 to make a query. After this conversion stored NSData in database is totally changed i.e. encoded again via UTF-8.
When I retrieved the data, it was not same as the old one. Therefore got error while un archiving it.
Solution: Stored NSData into sqlite3 without converting to UTF-8 and used sqlite_bind_blob for storing blob data to sqlite3 database.
Is there any reason for NSJSONSerialization to use NSData instead of NSString for representing JSON data?
NSString seems like a more obvious choice to me...
I imagine it would be more efficient to encourage parsing NSData instead of NSString. If you are parsing a response from a server, for example, you'll get an NSData object representing a buffer of raw bytes returned from the server (note that NSJSONSerialization also includes a method for parsing an NSInputStream directly). Parsing the whole thing into an NSString would be a waste since that would just be an intermediate object that would get thrown out. Instead, NSJSONSerialization is probably parsing the bytes in the NSData object directly and only construct NSStrings for the appropriate keys and values in the resulting data structure.
What's the reason that I can't parse a base64 string from a JSON request? when I make it a small string it works.
To clarify a little:
else if([connection isEqual:self.appearanceConnection]){
NSArray *arrayOfAppearances = [NSJSONSerialization JSONObjectWithData:[[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingAllowFragments error:&error];
NSLog(#"het aantal appearances is: %i", arrayOfAppearances.count);
[self syncAppearances:arrayOfAppearances];
}
When I edit it to a small string, I get the response that the length of the received array is 1. If I change it again to the base 64 of the image, the length is 0.
http://cl.ly/image/470Z0X1P3K1b (image form JSON response)
The error I get on the String:
Updated answer:
You now inform us that JSONObjectWithData is reporting an error:
Unterminated string around character 62
Character 62 is the start of the logo. I'm not seeing the end of the JSON in any of your screen snapshots. It looks like it's getting cut off.
You haven't shown us how you are populating data, but it looks almost like you're using a NSURLConnection but trying to parse in didReceiveData as opposed to waiting for the full results and only invoking the the JSON parse in connectionDidFinishLoading. NSURLConnection will break a long response into several calls to didReceiveData and you have to append all of those NSData to a single NSMutableData, and only try to parse it when it's done retrieving everything.
You either need to (a) show us the code where you're loading data and/or (b) share the full JSON. Either your JSON isn't properly terminated or you're trying to parse it before the whole thing is downloaded (probably the latter).
Original answer:
I'm not sure if this is the problem, but your line that says:
NSArray *arrayOfAppearances = [NSJSONSerialization JSONObjectWithData:[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingAllowFragments error:&error];
should simply be:
NSArray *arrayOfAppearances = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
The JSONObjectWithData method takes a NSData, not a NSString.
And, if you're not getting anything returned from this method, you should examine the contents of error and see what it says.
If you're still unable to figure out what the problem is, perhaps you can share the full JSON response with us (give us a URL or upload it somewhere) and we can take a look at it.
With a big thanks to #Rob!
Here a little summary:
Create a variable NSMutableData (don't forget to initialise in the viewdidload)
In the didReceiveData, you append the data to your mutable data using [self.appearancedata appendData:data];
In the connectionDidFinishLoading you parse your JSON
I have been working on a simple text editor in Cocoa/Objective-C for a practice project, and I have come across an error that I would never have expected.
I have an NSString for my file's contents as well as an NSString for it's path. When I attempt to write the contents to a file, I use the following method:
[FileContents writeToFile: CurrentFileName
atomically: NO
encoding: NSStringEncoding /* Error occurs on this line */
error: nil];
I've used this method many times without error yet today, I am getting an error:
Expected expression before 'NSStringEncoding'
NSStringEncoding isn't a valid value. You need to decide what text encoding to use. If you don't know anything about text encodings and these files are only used by your program, I would recommend using NSUTF8StringEncoding everywhere.
UTF-8 has many benefits, including that it is plain ASCII if you don't encounter any non-ASCII characters.
NSStringEncoding is a type, not a value. You need to specify which NSStringEncoding you want (e.g. NSUTF8StringEncoding, NSASCIIStringEncoding and so on).