AFNetworking sending URL as post parameter - ios

I am calling an API in which I am sending a URL link as a post parameter with it.
But while converting it into JSON data using
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:lParameters options:NSJSONWritingPrettyPrinted error:&error];
it adds extra '\' character into the link.
and when I log that data string, it logs like
data string : {
"id" : "1",
"photoLink" : "https:\/\/7.7.100.120:8443\/webresource\/carsevent\/gallery\/approved\/image-a31ea5e0-6284-402a-9e6a-b0cdba37bc1f.png"
}
Log :
Dictionary :
{
id = 1;
photoLink = "https://7.7.100.120:8443/webresource/carsevent/gallery/approved/image-a31ea5e0-6284-402a-9e6a-b0cdba37bc1f.png";
}
So API is returning error in this case.
How can I overcome from this?
Any help would be much appreciable.
Thanks

JSON must have certain characters escaped with a "\" character and even though "/" is not required to be escaped it is allowed to be escaped. Therefor the JSON with escaped "/" characters is valid and should be accepted by the API.
You can remove them if needed.
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\\/" withString:#"/"];
jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
File a bug report: http://bugreport.apple.com requesting an option to not escape "/".
See this SO answer.
See JSON for the characters that must be escaped.

Related

JSON serialization double quote (") and single (') quote issue in iOS

I'm facing JSON encoding issue.
I have submitted my data to server on below formate to save remarks.
{
Remarks = "test apple """ ";
}
Then while fetching the data from the server, I'm receiving different formate like
output:
{
Remarks = "test apple \U00e2\U0080\U009c\U00e2\U0080\U009d\U00e2\U0080\U009d\U00e2\U0080\U009d\\n";
}
While submitting data I'm using JSON serialisation.
NSData *jsonData
= [NSJSONSerialization dataWithJSONObject: dict
options: NSJSONWritingPrettyPrinted
error: nil];
if (jsonData)
{
NSString* jsonString
= [[NSString alloc]
initWithData: jsonData
encoding: NSUTF8StringEncoding];
NSLog(#"posting params: %#", jsonString);
}
My question is why I'm not getting I have submitted. I'm facing only double quote (") and single (') symbols.
Any one have idea?
You probably should escape your non-delimitating quotes :
{
Remarks = "test apple \"\"\" ";
}
iOS 11 has added "Smart Punctuation" to the keyboard settings.
This means when typing "Singapore" it will convert it to “Singapore”. The '"' are replaced by a '“' and '”' string. Those are different quotes.
So one option might be to also replace '“' and '”' by '\"'.
Smart Punctuation = No

Converting JSON string into object

I am trying to convert a JSON string into an object in Objective C using the code:
NSString *jsonString = (NSString *) responseObject;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
The value of jsonString is
"[{\"Date\": \"12-01-2015\", \"Time\": \"7:00 am\", \"Title\":
\"First Car Service\", \"Details\": \"This was the first car
service ever done\"}]"
But the value of json is always nil. How do I convert the jsonString into a NSArray ?
My (third) bet: your json string isn't correct. If it contains the leading and trailing quotes strip them. Replaced the \" with ".
better: make the server or other json source send correct json.
From the error, it may be that your string is not formatted correctly, probably due to the quotes.
You should also make sure that after you have formatted the string, then you check the array has objects.
NSLog(#"JSON Details: %#", json[0][#"Details"]);

NSJSONSerialization JSONObjectWithData where data contains line break characters

Sample Code:
NSString *jsonObject = ...;
BOOL isValidJSONObject = [NSJSONSerialization isValidJSONObject:jsonObject];
id jsonResponse = [NSJSONSerialization JSONObjectWithData:parsedData
options:NSJSONReadingAllowFragments
error:&jsonError];
Issue:
If jsonObject contains string data with newline characters, then NSJSONSerialization fails to parse and return a valid jsonRespone object. It returns following error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 119.) UserInfo=0x10ba9fef0 {NSDebugDescription=Unescaped control character around character 119.}
Sample JSON String (with newline character):
{"Name" : "Lorum","Description" : "Sample:
Lorum ipsum","Quantity" : 1,"Type" : 1}
What's the best way to handle this situation? Newline/ line break character should be valid in this case.
YES, double backslash is needed. However slash is not need to write twice.
For example,
jsonString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:#[#"line 0\nline1", #"http://twitter.com"] options:0 error:nil] encoding:NSUTF8StringEncoding];
when hover your cursor on jsonString, Xcode will tell you what it exactly want. If you write
jsonString = #"[\"http://twitter.com\"]",
you can get correct answer when using NSJSONSerialization to parse the string. Even you know the more suitable input is
jsonString = #"[\"http:\\/\\/twitter.com\"]".

Why does my NSDictionary of JSON objects have quotes around some values but not others? [duplicate]

This question already has answers here:
NSMutableDictionary is adding quotes to keys and values - why?
(2 answers)
Closed 9 years ago.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"Succeeded! Received %d bytes of data",[_configData length]);
NSString *responseJSONString = [[NSString alloc] initWithData:_configData encoding: NSASCIIStringEncoding];
NSLog(#"Response: %#", responseJSONString);
// convert to dictionary 'settingsDictionary'
NSError* error = [[NSError alloc] init];
NSDictionary *settingsDictionary = [NSJSONSerialization JSONObjectWithData:_configData options:kNilOptions error:&error];
NSLog(#"Dictionary of JSON objects ::: \n%#", settingsDictionary); // why?!
NSLog(#"DONE");
yeilds this in Output terminal:
2013-05-22 11:38:59.318 Tests[8817:c07] didReceiveResponse:
responseData length:(0) 2013-05-22 11:38:59.320 Tests[8817:c07]
Succeeded! Received 114 bytes of data 2013-05-22 11:38:59.321
Tests[8817:c07] Response: {"CustomerName":"Example Company","HostName":"streaming1.mycompany.com\/live","AppName":"streamer","Port":"1935"}
2013-05-22 11:38:59.321 Tests[8817:c07] Dictionary of JSON objects :::
{
AppName = streamer;
CustomerName = "Example Company";
HostName = "streaming1.mycompany.com/live";
Port = 1935;
}
2013-05-22 11:38:59.322 Tests[8817:c07] DONE
I don't understand why, if all the json values are enclosed in quotations, only 2/4 dictionary items include them. What is NSDictionary supposed to store by default?
Only those values are quoted in the description which aren't proper identifiers (i. e. there are spaces, special characters, and not just alphanumeric characters). The description of the dictionary doesn't print the keys and values as-is. (Specifically, they aren't actually quoted).
This has nothing to do with them being quoted in the JSON. In JSON, every string is quoted, always.
Strings with spaces and special characters like dot are surrounded by " to denote that it is single entity. Normal words and numbers are self explanatory.

Parse double quotes and single quotes in Json Parsing

I am parsing a JSON url. I am getting \u00e2\u20ac\u0153 instead of single quotes and \u00e2\u20ac\u009d instead of double quotes. On my end i am doing NSUTF8StringEncoding but still not getting single and double quotes.
Here is the response in the web browser:
{"status":"1","data":[{"id":"1345","title_en":"","content_en":"Mom
said, \u00e2\u20ac\u0153I love this one.\u00e2\u20ac\u009d\n\nJake
said, \u00e2\u20ac\u0153I don\u00e2\u20ac\u2122t get
it.\u00e2\u20ac\u009d\n","image":"1396","pos":"2","video_type":"0","video":"0","video_alt":"","sound_type":"1","sound":"1123","sound_alt":"","author":"","type":"0","book_id":"148","user_id":"24","title":"The
Art
Museum","author_url":"maureen-d-1","book_url":"the-art-museum","video_filename":"","sound_filename":"atmuseum2.m4a"}]}
Is it possible to parse above response using NSUTF8StringEncoding or something else. Or i need to get it done from php side.
Do you use NSJSONSerialization? In my app i do this:
NSError* localError;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&localError];
In "data" variable i have response from server. I receiving UTF-encoded text from server too (like \u00e2\u20ac) but finally i get normal string. "jsonObjects" will contain data in native types:
if ([jsonObjects isKindOfClass:[NSDictionary class]])
{
NSNumber* age = [jsonObjects objectForKey:#"age"];
//...etc
}

Resources