while parsing the content of a .json file, string like "jamie's" is represented as "jamie 's".Any one know why it is so?
Make sure you are using application/json as a content type to transfer the data from the server to the client.
You can also go through following link
http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/
because the apostrophe is a special charachter and cant be transmitted inside of http packets. all special characters must be replaced with escape sequences like ' or with percent escapes %27 ...
in the NSString class you will find the methods 'stringByReplacingPercentEscapesUsingEncoding:' and 'stringByAddingPercentEscapesUsingEncoding:' for handle percent escapes.
the escape sequencies you must handle by yourself for example with 'stringByReplacingOccurrencesOfString:#"##039;" withString:#"'"' ...
Related
I have a string which is a base64encoding of an audio file and looks like -"\/qwerty\/\/\/qwertyuiop". However, my server side developer says he just receives -"/qwerty///qwertyuiop". Which does not give the same result in decoding. So in what format do i send him that he will receive -"\/qwerty\/\/\/qwertyuiop" ? Please help.
I think what you want is to escape "\" from your URL. You can use BackSlash Escape Character.
Update :
You can use the Answer from these links :
URL encode an NSString
How do I do base64 encoding on iphone-sdk ?
Also take a look at this Beautiful Link : HTML URL Encoding Reference (Credit goes to HotLicks)
If I understood you correctly, here's what you need:
\\/qwerty\\/\\/\\/qwertyuiop
More about the escaping characters you can read here:Wiki
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 trying to display “Administrative File & Express” but it is displaying as "Express". So I am unable to show anything that is before the “&”.
You need to escape chars like '&' in XML Parsing. See following link...
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
What characters do I need to escape in XML documents?
Now check XML you are receiving. if you are not receiving chars with escape sequence then you need to handle it in your code.....
Write here if you need further details.....
I am trying to parse some data using nsxmlparser, whenever there is a &(ampersand) present in the text being received it just stops reading the parsed data. How can I read & normally, similar to other normal characters.
Thanks
Pankaj
A lone ampersand in an XML document is not valid except in a CDATA section. You can either have your XML provider provide valid XML by either:
Using the & character entity where you want ampersands.
Putting text containing ampersands into a CDATA section.
Could not find the solution so i had to replace the & with some characters in backend and then again replace it in iphone while using it
Apologies if this has been answered already. There are similar topics but none that I could find pertaining to Cocoa & NSStrings...
I'm constructing a clickable URL to embed in an HTML email to be sent via the MFMailComposeViewController on the iPhone. i create the url then use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding to polish up white space, etc. then add some surrounding HTML to get:
view
All's well so it's appended to emailBody. However once [mailComposer setMessageBody:emailBody isHTML:YES] all the & become & which isn't ideal within my URL.
can i control this? is there a better encoding algorithm? my HTML is a bit rusty perhaps I'm using the wrong encoding? I'm sure on the server I could parse the & back into & but looking for the Cocoa way...
Thanks!
Actually, & should always be encoded as & in HTML attributes. Including links. Including form value delimiters. So it's done exactly what you want, even though you didn't know you wanted it.
Look at it this way: in your URL, you have &age=53... That's interpreted first as a character entity, and only after that doesn't work is it interpreted as an ampersand followed by more character data.
The W3C spec is quite clear on this:
Authors should use "&" (ASCII decimal 38) instead of "&" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&" in attribute values since character references are allowed within CDATA attribute values.
That should settle it: use & not &.
Are you calling MFMailComposeViewController's
setMessageBody:isHTML:
and what do you set isHTML to?
Depending on it's setting it might very well be that MFMailComposeViewController is trying to help you out be encoding the entire message body...
Either don't encode the body yourself or make the entire body HTML.