i'm saving a NSString inside an NSArray and that NSArray inside an NSDictionary. While doing this, a process inside my NSDictionary notifies me if my string is like Hi I'm XYZ. Then in the place of single quote the appropriate UTF character is getting stored.
So how to avoid this or how can I get my actual text along with special characters from NSArray or from my NSDictionary?
Any help is thankful.
NSString internally uses Unicode characters. So it easily can handle all sorts of characters from different languages.
You cannot choose the internal encodig of NSString. It's always Unicode. If you have an encoding problem, then you have either created the NSString instance incorrectly or you have output the instance the wrong way.
And there's no such thing as an UTF character.
Please better describe your problem and show the relevant source code.
Related
I have some xml that is coming back from a web service. I in turn use xslt to turn that xml into json (I am turning someone else's xml service into a json-based service). My service, which is now outputting JSON, is consumed by my iphone app using the de facto iphone json framework, SBJSON.
The problem is, using the [string JSONValue] method chokes, and I can see that it's due to line breaks. Lo and behold, even the FAQ tells me the problem but I don't know how to fix it.
The parser fails to parse string X
Are you sure it's legal JSON? This framework is really strict, so won't accept stuff that (apparently) several validators accepts. In particular, literal TAB, NEWLINE or CARRIAGE RETURN (and all other control characters) characters in string tokens are disallowed, but can be very difficult to spot. (These characters are allowed between tokens, of course.)
If you get something like the below (the number may vary) then one of your strings has disallowed Unicode control characters in it.
NSLocalizedDescription = "Unescaped control character '0x9'";
I have tried using a line such as: NSString *myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"];
But that doesn't work. My xml service is not coming back as CDATA. The xml does have a line break in it as far as I can tell (how would I confirm this). I just want to faithfully transmit the line break into JSON.
I have actually spent an entire day on this, so it's time to ask. I have no pride anymore.
Thanks alot
Escaping a new line character should work. So following line should ideally work. Just check if your input also contains '\r' character.
NSString *myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"];
You can check which control character is present in the string using any editor which supports displaying all characters (non-displayable characters as well). e.g. using Notepad++ you can view all characters contained in a string.
It sounds like your XSLT is not working, in that it is not producing legal JSON. This is unsurprising, as producing correctly formatted JSON strings is not entirely trivial. I'm wondering if it would be simpler to just use the standard XML library to parse the XML into data structures that your app can consume.
I don't have a solution for you, but I usually use CJSONSerializer and CJSONDeserializer from the TouchJSON project and it is pretty reliable, I have never had a problem with line breaks before. Just a thought.
http://code.google.com/p/touchcode/source/browse/TouchJSON/Source/JSON/CJSONDeserializer.m?r=6294fcb084a8f174e243a68ccfb7e2c519def219
http://code.google.com/p/touchcode/source/browse/TouchJSON/Source/JSON/CJSONSerializer.m?r=3f52118ae2ff60cc34e31dd36d92610c9dd6c306
I am using TouchJSON to parse a JSON data.
In the results, the strings in the array are all in \U** format.
Yes, they are meant to be other languages than English.
Why TouchJSON can't just replace them with real string via UTF8?
How should I deal with the results if I want to store them as NSStrings and use them in UILabel?
Thanks
The leading \u should not be a part of the string, but rather an identifier. If you load an NSString from parsed NSDictionary, it should've ignored the leading \u.
If it did not, you can always use [theString substringFromIndex: 2] to remove any leading identifiers.
Otherwise, take a look at SBJson, an alternative library for Objective-C JSON parsing.
when I try to write this JSON:
{"author":"mehdi","email":"email#hotmail.fr","message":"Hello"}
like this in Objective-C:
NSString *myJson=#"{"author":"mehdi","email":"email#hotmail.fr","message":"Hello"}";
it doesn't work. Can someone help me?
You need to escape quote characters with a backslash:
NSString *myJson = #"{\"author\":\"mehdi\",\"email\":\"email#hotmail.fr\",\"message\":\"Hello\"}";
Otherwise the compiler will think that your string literal ends right after the first {.
The backslashes will not be present as characters in the resulting NSString. They are merely there as hints for the compiler and are removed from the actual string during compilation.
Newbie note: JSON strings that you read directly from a file via Objective C of course do not need any escaping! (JSON itself may need such, but that's about it. No need for additional escaping on the ObjC-side of it.)
I have implementing pdf parsing in which i have parsed pdf and fetch the all text but it disply junks characters so i want to convert in to utf string.How it possible please help me for this question.
First, you need to find out which encoding is currently used for the text. I guess it's ISO-8859-1, aka Latin-1 or it's variant ISO-8859-15, aka Latin-15.
As soon as know that it's a piece of cake. You haven't said in which container you got the text, e.g. whether it's stored in a C string or NSData.
Let's assume you got a C string. In that case you would do:
myString = [[NSString alloc] initWithBytes:myCString
length:strlen(myCString)
encoding:NSISOLatin1StringEncoding];
If you got a NSData, you would use the initWithData:encoding: initializer instead. That's all you need to do, as according to Apple's documentation, "A string object presents itself as an array of Unicode characters". If you need a UTF8-encoded C string, you can then query it via:
myUTF8CString = [myString UTF8String];
There's also dataUsingEncoding: to get a NSData object instead of a C string.
Have a look at the NSString class reference and the NSStringEncoding constants.
I read from a sqlite db to my iphone app. Within the texts sometimes there are special characters like 'xf2' or 'xe0' as I can see in the debugger in the char* data type. When I try to transform the chars to an NSString Object by using initWithUTF8String, I get a nil back.
How can I transform such special characters?
It looks like encoding issue. You can get 'xf2' or 'xe0' when you have such symbols as © or ®. Those symbols need 2 bytes, and sqlite can interpret each byte of symbol as separate symbol.
So, try to use not initWithUTF8String, but initWithCString:
NSString *stringFromDB = [[NSString alloc] initWithCString:charsArrayFromDB
encoding:NSASCIIStringEncoding];