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
Unable to parse json string
I have got the following hard coded json. I tried converting it to dictionary and parse it but Im unable to do so.
NSString *hcResponse = #"{\"status\":1,\"value\":\"Successfully Login\"}";
How to parse such a json into key value pairs?
Finally, I've figured it by myself
//convert the string to nsdata
NSData *jsonData = [hcResponse dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
//convert the data to json dictionary
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
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]);
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?
I have a JSON string as an NSString object in iOS. I want to parse this and pull out the given parameters in the JSON string. Is there a efficient way to parse this or is the only way to search for substrings etc.?
The way to do it with iOS 5 is to use the NSJSONSerialization class. You will want to first convert your string to an NSData object, and call the class method JSONObjectWithData
NSData *jsonData = [myJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
Note that JSONObjectWithData will return either an NSDictionary or an NSArray, depending whether your JSON string represents an a dictionary or an array.
A good framework for converting JSON strings to Objective-C objects (NSArray and NSDictionary) is SBJson (Github).
Usage:
NSDictionary *dict = [myJsonString JSONValue];