I am getting NSString * responseString as below format which is already utf8 encoded. Data is from webserver database.
NSString *jsonString = #"{\"ID\":{\"Content\":\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13,\"type\":\"text\"}}";
//Trying to convert to UTF
NSString *utf = [jsonString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//convert to data
NSData *data = [utf dataUsingEncoding:NSUTF8StringEncoding];
//trying to create NSDictionary JSON
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"json: %#", son);
NSLog(#"utf: %#", utf);
I NEED TO CONVERT IT INTO NSDICTIONARY SO I CAN PARSE AND GET Value of Content
Output Results:
utf: (null)
utf: {"ID":{"Content":ฉันรักคุณ,"type":"text"},"ContractTemplateID":{"Content":ฉันรักคุณ,"type":"text"}}
Strings will present as Unicode in debugger. Just show them with labels or convert into characters with some online tools.
Related
My response contains image with base64String.
NSError *jsonError = nil;
id jSon = [NSJSONSerialization JSONObjectWithData:data options:(0) error:&jsonError];
Error text is
"The data couldn’t be read because it isn’t in the correct format.
"
Please help me to parse this data.
If I transform the response I broke image base64String, then I can't load it.
NSString *stringData = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
NSString *str1 = [stringData stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSString *str2 = [str1 stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSString *str = [str2 stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
jsonError = nil;
jSon = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
The problem is that the base64 encoded string contains linefeed characters which have to be escaped in a JSON string. If you are responsible for the server side send the base64 encoded string without inserting a line feed character after every 64 characters.
Otherwise it's sufficient to remove the linefeed characters
NSString *stringData = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
NSString *str1 = [stringData stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSData *data = [str1 dataUsingEncoding:NSUTF8StringEncoding];
jsonError = nil;
jSon = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
I have data received from server in JSON format. It is translated to NSDictionary from which I am taking string value. Problem is when I try to print that name I am expecting "ö", but getting "ö". In UTF-8, "ö" is represented as 0xC3B6. In UTF-16, "Ã" is 0xC3 and "¶" is 0xB6. What am I doing wrong so dictionary value is represented in UTF-16 instead of UTF-8? Here is code that I am using.
NSString *inptu = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
NSLog(#"input: %#",inptu);
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
//get json from data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *value = [json valueForKey:"name"];
NSLog(#"output: %#",value);
For first log I get string like this: "input: \u00C3\u00B6"
For second: "output: ö"
This issue can be repeated with all other extended ASCII characters.
JSON seems to be encoded in Latin-1, you should check that and try with NSISOLatin1StringEncoding:
NSData *data = [input dataUsingEncoding:NSISOLatin1StringEncoding];
Problem: I can't parse data from a JSON file to a NSArray appropriately. UTF encoding is not working as expected.
My JSON looks something like:
[
{"Name":"Marcos","Address":"1234 Brasil Av. São Paulo - SP","Latitude":"-23.000","Longitude":"-46.70"},{"Name":"Mario","Address":"1000 Washignton Luiz Av. Itú SP","Latitude":"-20.0000","Longitude":"-46.000"}
]
My Objective-C code is:
NSError *error = nil;
NSURL *jsonUrl = [[NSURL alloc]initWithString:
#"http://marcosdegni.com.br/teste/webservice_teste.php"];
NSString *jsonString = [NSString stringWithContentsOfURL:jsonUrl
encoding:NSUTF8StringEncoding error:&error];
NSLog(#"jsonString: %# , Error:%#:" ,jsonString, error); //(1)
if (!error) {
NSError *error2 = nil;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray * jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error2];
NSLog(#"\n\nArray: %#" \nError:$#, jsonArray, error2); //(2)
//(*1*) This log show the content's as they are expected: note the characters ã and ú on the address fields.
//(*2*) The logs from the array and the dictionary show this charters as it's UNIX codes:\U00e and \U00fa respectively.
You can give this a try. The id json you get will be a NSArray, you can use it from there.
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray * array = json;
for (NSDictionary *dict in array) {
NSString *string = [dict objectForKey:#"Address"];
NSLog(#"%#",string);
}
From here, and I get the right result if I obtain the value of the key and log it, instead of logging the NSArray directly.
I try to JSON encode NSDictionary into NSArray upload to PHP web-services.
This is how I JSON encode my NSArray:
NSError * error;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:my_array_1 options:0 error:&error];
NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
Then I insert this JSON String into another array by:
NSString * jsonRequest = [NSString stringWithFormat: #"{\"request\":\"syncBookmark\",\"bookmark_array\":\"%#\",\"user_id\":\"%#\"}", jsonString, user_id, nil];
NSData * requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
Before I request the webservices, the JSON encoded string is like:
{"request":"syncBookmark","bookmark_array":"[{\"PostId\":\"4\",\"last_edit_date\":\"2014-03-03 01:05:37\",\"BookmarkId\":\"1\",\"last_sync_date\":\"\",\"is_active\":\"1\"}]","user_id":"7"}
I can echo the 'user_id' in PHP which is '7' correctly, but when I check the array - is_array($bookmark_array) in PHP it just return FALSE. Am I doing the way wrongly to put arrays in array?
This is because you enter JSON string as string and it is obviously escaped by the stringWithFormat method.
You should create a NSDictionary and serialize that together. So instead of serializing my_array_1 variable, you will serialize the dictionary.
Create the dictionary:
NSDictionary* dictionary = #{ #"request" : #"syncBookmark", #"bookmark_array" : my_array_1 };
Then serialize the dictionary:
NSError * error;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
This is the correct way to serialize a JSON object, not inserting serialized objects into another string. Although I see why you wanted to do that.
Don't bother doing the stringByReplacingOccurrencesOfString stuff. Let NSJSONSerialization do all of the necessary quoting for you.
So, if you really need that jsonString as a string that your PHP will recursively un-encode, just JSON encode the array, and then put that in another dictionary and then encode that again:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:my_array_1 options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *dictionary = #{#"request" : #"syncBookmark",
#"bookmark_array" : jsonString,
#"user_id" : user_id};
NSData *finalData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
That should create a payload much like you said you wanted in your question.
Having said that, a more logical approach would be:
NSError *error;
NSDictionary *dictionary = #{#"request" : #"syncBookmark",
#"bookmark_array" : my_array_1,
#"user_id" : user_id};
NSData *finalData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
If that's what generated your payload, then your PHP wouldn't have to do multiple calls to json_decode.
How to convert NSDictionary to NSString which contains JSON of NSDictionary ?
I have tried like but without success
//parameters is NSDictionary
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
options:0
error:&error];
if jsonData is NSDictionary
NSString *str=[NSString stringWithFormat:#"json data is %#", jsonData];
OR if jsonData is NSData
NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];
If you just want to inspect it, you can create a NSString:
NSString *string = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
But if you're writing it to a file or sending it to a server, you can just use your NSData. The above construct is useful for examining the value for debugging purposes.