Cocoa error 3840 using JSON non latin characters being parsed - ios

I already checked and my JSon is valid.
I try to pass it off to my app with this code
NSURL *actualURL = [NSURL URLWithString:[serverURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *errorJson;
NSData *data = [NSData dataWithContentsOfURL:actualURL options:kNilOptions error:nil];
jsonData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errorJson];
I am getting the 3840 error code in the NSJSONSerialization
jsondata is (nonatomic, strong) NSMutableArray btw.
I tried many solutions I found online but it isn't working.
Does anyone have any ideas? I think the problem might be that some of the data being parsed are Greek characters (UTF8_unicode_cl collation on the server).
Here's my JSon example data btw
[{"diseaseID":"18","diseaseName":"test disease 1","diseaseDescription":"test disease 1 description","diseaseDoctor":"\u03ba\u03b1\u03c1\u03b4\u03b9\u03bf\u03bb\u03cc\u03b3\u03bf\u03c2","diseaseImagePath":"test disease 1"}]
Any ideas?

Related

NSDictionary Sorting keys by default

I am working on API integration, From API I am getting the response in dictionary format, While I serialise that data, it is sorting automatically
Here is my code
id resposeJSon = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
While I try to print the same in NSString it works as the response is received
NSString *jsonString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
I am expecting the serialised variable resposeJSon should be in the same format what I get from variable jsonString
Thanks in advance

How to parse {status:0,val:450} from a HTML response

i want to take 450
from the following:
NSString {status:0,val:450}
using objective c:
{status:0,val:450}
Please suggest me answer
I cannot completely understand your question . I think you have you have an JSON data like:
{
status:0;
val:450;
}
If you want this data in your app. You want to do
NSURL *blogURL = [NSURL URLWithString:#"https://your url here"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *data = [dataDictionary objectForKey:#"status"];
NSDictionary *item = [data objectForKey:#"val"];
Your JSON Data get in Dictionary format. You can access the Data using the Key. In here your keys are status and val.
I hope this links are help for you.
fetch parse json ios programming tutorial
and this Link:
json parsing in ios

Getting element from NSString

my app get data from a php web service and return a NSString
["first name","last name","adress","test#test","000-000-0000","password","code","0"]
How can i get the second element ?
This is a JSON formatted string which you are getting from your web service.
You must be getting bytes from server. Just replace your variable which have bytes stored with my variable "response data".
Code:
NSError* error;
NSArray* myResultArray = [NSJSONSerialization
JSONObjectWithData:responseData options:kNilOptions error:&error];
You will get an array in variable "myResultArray" and you can get all value by index.
Code:
NSString* first name = [myResultArray objectAtIndex:0];
What you have given here is an array and not a string. May be you could provide more details like the exact response and the code that you are trying here.
To Convert a JSON string to NSDictionary all you need to do is:
NSError *jsonError;
NSData *objectData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:nil
error:&jsonError];
And to NSArray :
NSArray *array = [NSJSONSerialization JSONObjectWithData:objectData options:nil error:&jsonError];
NSString *secondElement = array[1];
That web service doesn't return an NSString. It runs data in some format defined by the service, and you convert it to an NSString. Find out what the format actually is, then convert it appropriately, for example using NSJSONSerialization.
The example string your showed here seems wired. This looks more like a array than string. If this is a NSArray then you can do it like this:
NSArray *data = #[#"first name",#"last name", #"adress", #"test#test", #"000-000-0000", #"password", #"code", #"0"];
NSLog (#"Second component = %#", data[1]);
However, if you anticipate this as NSString then this is how you would handle this:
NSString *test = #"[\"first name\",\"last name\",\"adress\",\"test#test\",\"000-000-0000\",\"password\",\"code\",\"0\"]";
NSLog (#"Second component = %#", [test componentsSeparatedByString:#","][1]);

Get string out of json object of only string

I have a Json string which is only a string in Json format, like below:
#""JSONContent""
Is there a way to get the content out from such JSON string? I've tried to use below code but it cannot be parsed to Dictionary
NSData *jsonData = [rawTime dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
First of all, if that is the reponse that you are getting, then probably it wont be recognized as JSON because JSON strings start with a '[' or '{' character. Hence, it wont be parsed. You have to make sure that the string you receive from the server is in proper JSON format or not. You can check the JSON string validation on this website : jsonlint.com
Now the code that you have tried will also not work because of the same reason, i.e improper JSON format. There can be many reasons so as to why JSON that is received is in a bad format. It also might be possible that your JSON might be embeeded in an XML string like as shown :
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">
[{"Title":"DemoTitle","CreationDate":"06/06/2014","Description":"DemoDescription"}]
</string>
This is just an example. So the conclusion here is : Make sure your JSON is in proper format, i.e starting with '[' or '{'.
As for the parsing code, this might help you :
NSString *link = [NSString stringWithFormat:#"yourServiceURL"];
NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:encdLink];
NSData *response = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
This will fetch the JSON response from the service URL and then parse it using NSJSONSerialization and finally store the data in an array for further use.
Hope this helps.

NSJSONSerialization doesn't parse this JSON

I'm trying to parse this JSON that looks to me like well written, but NSJSONSerialization doesn't think the same AFAIK since it's returning an NSArray.
This is my code:
NSData* gamesData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://s42sport.com/polarice/json/games.json"]
];
NSDictionary* json = nil;
if (gamesData) {
json = [NSJSONSerialization
JSONObjectWithData:gamesData
options:kNilOptions
error:nil];
NSLog(#"%d",json.count);
}
The questions are,
What's wrong with the JSON? Why NSSerialization doesn't return me the NSDictionary?
Edit: Yes, I just learned about the [...] vs {...}. Thank You.
Parse your json by this way.
NSURL * url=[NSURL URLWithString:#"http://s42sport.com/polarice/json/games.json"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary * json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSLog(#"%#",json);
NSArray * array1=[json valueForKey:#"c"];
NSLog(#"%#",array1);
Try this code. this will surely work for you.
NSDictionnary should be used for Object whereas NSArray is use for JSON array
NSArray* json = nil;
if (gamesData) {
json = [NSJSONSerialization
JSONObjectWithData:gamesData
options:kNilOptions
error:nil];
NSLog(#"%d",json.count);
}
The JSON file you listed is an array (it starts and ends with a square bracket) so Objective-C reflects that with an NSArray root object.

Resources