This question already has answers here:
Get values from NSDictionary
(3 answers)
Closed 6 years ago.
I am new to objective-c and iOS development. I need to get json values from a url and get values out of it.
This is the json value I am getting.
"\"[{\\\"VisitorId\\\":\\\"2270d224-2a82-4371-ae8e-43b98763eda6\\\",\\\"OperatorID\\\":null,\\\"VisitorSignalId\\\":\\\"06c6b36f-0d05-4f0c-bdee-1744c2cb6519\\\",\\\"Type\\\":\\\"Visitor\\\",\\\"Messagetext\\\":\\\"baman123 has joined the chat!\\\",\\\"IsJoin\\\":true,\\\"MessageTime\\\":\\\"2017-01-09 06:25:47\\\",\\\"File\\\":null,\\\"MessageType\\\":\\\"JoinMessage\\\",\\\"Name\\\":\\\"baman123\\\",\\\"Email\\\":\\\"baman123#gmail.com\\\",\\\"IP\\\":\\\"123.231.122.233\\\",\\\"Page\\\":\\\"http://www.sathyabaman.com/\\\",\\\"Rating\\\":\\\"No Rating\\\",\\\"Image\\\":null,\\\"CurrentOpImage\\\":null,\\\"FileUploadDetails\\\":null,\\\"FirstOperatorName\\\":null,\\\"OperatorCount\\\":1,\\\"RequestTranscript\\\":false,\\\"Country\\\":null,\\\"OS\\\":null,\\\"Broswer\\\":null,\\\"Mobile\\\":null,\\\"iso_code\\\":null},{\\\"VisitorId\\\":\\\"2270d224-2a82-4371-ae8e-43b98763eda6\\\",\\\"OperatorID\\\":null,\\\"VisitorSignalId\\\":null,\\\"Type\\\":\\\"Visitor\\\",\\\"Messagetext\\\":\\\"hi this is baman\\\",\\\"IsJoin\\\":false,\\\"MessageTime\\\":\\\"2017-01-09 08:01:07\\\",\\\"File\\\":null,\\\"MessageType\\\":\\\"NormalMessage\\\",\\\"Name\\\":\\\"baman123\\\",\\\"Email\\\":\\\"baman123#gmail.com\\\",\\\"IP\\\":\\\"123.231.122.233\\\",\\\"Page\\\":\\\"http://www.sathyabaman.com/\\\",\\\"Rating\\\":\\\"No Rating\\\",\\\"Image\\\":null,\\\"CurrentOpImage\\\":null,\\\"FileUploadDetails\\\":null,\\\"FirstOperatorName\\\":null,\\\"OperatorCount\\\":1,\\\"RequestTranscript\\\":false,\\\"Country\\\":null,\\\"OS\\\":null,\\\"Broswer\\\":null,\\\"Mobile\\\":null,\\\"iso_code\\\":null},{\\\"VisitorId\\\":\\\"2270d224-2a82-4371-ae8e-43b98763eda6\\\",\\\"OperatorID\\\":null,\\\"VisitorSignalId\\\":null,\\\"Type\\\":\\\"Visitor\\\",\\\"Messagetext\\\":\\\"are you there\\\",\\\"IsJoin\\\":false,\\\"MessageTime\\\":\\\"2017-01-09 08:01:10\\\",\\\"File\\\":null,\\\"MessageType\\\":\\\"NormalMessage\\\",\\\"Name\\\":\\\"baman123\\\",\\\"Email\\\":\\\"baman123#gmail.com\\\",\\\"IP\\\":\\\"123.231.122.233\\\",\\\"Page\\\":\\\"http://www.sathyabaman.com/\\\",\\\"Rating\\\":\\\"No Rating\\\",\\\"Image\\\":null,\\\"CurrentOpImage\\\":null,\\\"FileUploadDetails\\\":null,\\\"FirstOperatorName\\\":null,\\\"OperatorCount\\\":1,\\\"RequestTranscript\\\":false,\\\"Country\\\":null,\\\"OS\\\":null,\\\"Broswer\\\":null,\\\"Mobile\\\":null,\\\"iso_code\\\":null},{\\\"VisitorId\\\":\\\"2270d224-2a82-4371-ae8e-43b98763eda6\\\",\\\"OperatorID\\\":null,\\\"VisitorSignalId\\\":null,\\\"Type\\\":\\\"Visitor\\\",\\\"Messagetext\\\":\\\"fuck u\\\",\\\"IsJoin\\\":false,\\\"MessageTime\\\":\\\"2017-01-09 08:01:14\\\",\\\"File\\\":null,\\\"MessageType\\\":\\\"NormalMessage\\\",\\\"Name\\\":\\\"baman123\\\",\\\"Email\\\":\\\"baman123#gmail.com\\\",\\\"IP\\\":\\\"123.231.122.233\\\",\\\"Page\\\":\\\"http://www.sathyabaman.com/\\\",\\\"Rating\\\":\\\"No Rating\\\",\\\"Image\\\":null,\\\"CurrentOpImage\\\":null,\\\"FileUploadDetails\\\":null,\\\"FirstOperatorName\\\":null,\\\"OperatorCount\\\":1,\\\"RequestTranscript\\\":false,\\\"Country\\\":null,\\\"OS\\\":null,\\\"Broswer\\\":null,\\\"Mobile\\\":null,\\\"iso_code\\\":null},{\\\"VisitorId\\\":\\\"2270d224-2a82-4371-ae8e-43b98763eda6\\\",\\\"OperatorID\\\":null,\\\"VisitorSignalId\\\":null,\\\"Type\\\":\\\"Visitor\\\",\\\"Messagetext\\\":\\\"sathyabaman\\\",\\\"IsJoin\\\":false,\\\"MessageTime\\\":\\\"2017-01-09 08:37:15\\\",\\\"File\\\":null,\\\"MessageType\\\":\\\"NormalMessage\\\",\\\"Name\\\":\\\"baman123\\\",\\\"Email\\\":\\\"baman123#gmail.com\\\",\\\"IP\\\":\\\"123.231.122.233\\\",\\\"Page\\\":\\\"http://www.sathyabaman.com/\\\",\\\"Rating\\\":\\\"No Rating\\\",\\\"Image\\\":null,\\\"CurrentOpImage\\\":null,\\\"FileUploadDetails\\\":null,\\\"FirstOperatorName\\\":null,\\\"OperatorCount\\\":1,\\\"RequestTranscript\\\":false,\\\"Country\\\":null,\\\"OS\\\":null,\\\"Broswer\\\":null,\\\"Mobile\\\":null,\\\"iso_code\\\":null}]\""
And I tried to get values out
+ (void) showCurrentHistory:(NSString *) historyString {
NSLog(#"Received String : %#", historyString);
NSData *data = [historyString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
for (NSDictionary *dic in jsonArray) {
NSString *VisitorId = [dic objectForKey:#"VisitorId"];
NSString *VisitorSignalId = [dic objectForKey:#"VisitorSignalId"];
NSString *Type = [dic objectForKey:#"Type"];
NSString *Messagetext = [dic objectForKey:#"Messagetext"];
NSString *MessageTime = [dic objectForKey:#"MessageTime"];
NSString *Image = [dic objectForKey:#"Image"];
NSString *FileUploadDetails = [dic objectForKey:#"FileUploadDetails"];
NSString *FirstOperatorName = [dic objectForKey:#"FirstOperatorName"];
NSString *Rating = [dic objectForKey:#"Rating"];
NSLog(#"----------------------- Current Chat History --------------------------------");
NSLog(#"VisitorId : %#" , VisitorId);
NSLog(#"VisitorSignalId : %#", VisitorSignalId);
NSLog(#"Type : %#" , Type);
NSLog(#"Messagetext : %#" , Messagetext);
NSLog(#"MessageTime : %#" , MessageTime);
NSLog(#"Image : %#", Image);
NSLog(#"FileUploadDetails : %#" , FileUploadDetails);
NSLog(#"FirstOperatorName : %#" , FirstOperatorName);
NSLog(#"Rating : %#" , Rating);
NSLog(#"------------------------------------------------------------------------------");
}
}
But its not entering the for loop. Can someone help me to fix this tnx.
Your JSON String may contains extra \\ so you have to remove that one.
Please check below JSON string and code.
NSString *str = #"[{\"VisitorId\":\"2270d224-2a82-4371-ae8e-43b98763eda6\",\"OperatorID\":null,\"VisitorSignalId\":\"06c6b36f-0d05-4f0c-bdee-1744c2cb6519\",\"Type\":\"Visitor\",\"Messagetext\":\"baman123 has joined the chat!\",\"IsJoin\":true,\"MessageTime\":\"2017-01-09 06:25:47\",\"File\":null,\"MessageType\":\"JoinMessage\",\"Name\":\"baman123\",\"Email\":\"baman123#gmail.com\",\"IP\":\"123.231.122.233\",\"Page\":\"http://www.sathyabaman.com/\",\"Rating\":\"No Rating\",\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":null,\"OperatorCount\":1,\"RequestTranscript\":false,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":\"2270d224-2a82-4371-ae8e-43b98763eda6\",\"OperatorID\":null,\"VisitorSignalId\":null,\"Type\":\"Visitor\",\"Messagetext\":\"hi this is baman\",\"IsJoin\":false,\"MessageTime\":\"2017-01-09 08:01:07\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"baman123\",\"Email\":\"baman123#gmail.com\",\"IP\":\"123.231.122.233\",\"Page\":\"http://www.sathyabaman.com/\",\"Rating\":\"No Rating\",\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":null,\"OperatorCount\":1,\"RequestTranscript\":false,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":\"2270d224-2a82-4371-ae8e-43b98763eda6\",\"OperatorID\":null,\"VisitorSignalId\":null,\"Type\":\"Visitor\",\"Messagetext\":\"are you there\",\"IsJoin\":false,\"MessageTime\":\"2017-01-09 08:01:10\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"baman123\",\"Email\":\"baman123#gmail.com\",\"IP\":\"123.231.122.233\",\"Page\":\"http://www.sathyabaman.com/\",\"Rating\":\"No Rating\",\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":null,\"OperatorCount\":1,\"RequestTranscript\":false,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":\"2270d224-2a82-4371-ae8e-43b98763eda6\",\"OperatorID\":null,\"VisitorSignalId\":null,\"Type\":\"Visitor\",\"Messagetext\":\"fuck u\",\"IsJoin\":false,\"MessageTime\":\"2017-01-09 08:01:14\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"baman123\",\"Email\":\"baman123#gmail.com\",\"IP\":\"123.231.122.233\",\"Page\":\"http://www.sathyabaman.com/\",\"Rating\":\"No Rating\",\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":null,\"OperatorCount\":1,\"RequestTranscript\":false,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":\"2270d224-2a82-4371-ae8e-43b98763eda6\",\"OperatorID\":null,\"VisitorSignalId\":null,\"Type\":\"Visitor\",\"Messagetext\":\"sathyabaman\",\"IsJoin\":false,\"MessageTime\":\"2017-01-09 08:37:15\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"baman123\",\"Email\":\"baman123#gmail.com\",\"IP\":\"123.231.122.233\",\"Page\":\"http://www.sathyabaman.com/\",\"Rating\":\"No Rating\",\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":null,\"OperatorCount\":1,\"RequestTranscript\":false,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null}]";
NSData *data = [[NSData alloc] initWithBytes:[str UTF8String] length:str.length];
NSError *error;
NSMutableArray response = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
Hope this will helps to solve your problem.
You are getting json string, you can convert json object from it like,
id jsonString = yourJsonStringHere;
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#",jsonObject);
After get string from web service, i need to parse them. But something is going wrong.
Here is my code;
NSString *responseString = [request responseString];
NSLog(#"response String = %#",responseString);
NSData *tempData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSString *innerJson = [NSJSONSerialization JSONObjectWithData:tempData
options:NSJSONReadingAllowFragments error:&error];
NSLog(#"innerJson = %#",innerJson);
NSArray *entries = [NSJSONSerialization JSONObjectWithData:[innerJson dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
NSLog(#"entries = %#",entries);
for (NSDictionary *entry in entries) {
NSLog(#"entry = %#",entry);
NSString *message = [entry objectForKey:#"message"];
NSLog(#"message = %#",message );
NSString* result = [entry objectForKey:#"result"];
NSLog(#"result = %#", result);
}
Here is my output;
innerJson = {"result": false,"message":"message!"}//I need parse this string.
entries = {
message = "message!";
result = 0;
}
entry = message
I am taking error in for loop.
What am i doing wrong?
Thanks for interest and advice.
The problem is that in the line
NSArray *entries = [NSJSONSerialization
you assign the result to NSArray, and your JSON object is a dictionary, so you should assign to NSDictionary. Then the for loop is unnecessary - you can do:
NSDictionary *entries = [NSJSONSerialization JSONObjectWithData:[innerJson dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];
NSLog(#"entries = %#",entries);
NSString *message = [entries objectForKey:#"message"];
NSLog(#"message = %#",message );
NSString* result = [entries objectForKey:#"result"];
NSLog(#"result = %#", result);
Read more details in NSJSONSerialization documentation.
I'm not sure - but I guess this code should work for you:
NSString *responseString = [request responseString];
NSLog(#"response String = %#",responseString);
NSData *tempData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
/* assuming it is a dictionary */
NSDictionary *inner = [NSJSONSerialization JSONObjectWithData:tempData
options:NSJSONReadingAllowFragments error:&error];
NSLog(#"inner = %#",inner);
for(id key in inner) {
id value = [inner objectForKey:key];
NSLog(#"key=%#, value=%#", key, value);
}
Please help me debug this code
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *urls = [NSURL URLWithString:[NSString stringWithFormat:#"http://cnapi.iconnectgroup.com/api/UserProfile?id=1"]];
NSString *json = [NSString stringWithContentsOfURL:urls encoding:NSASCIIStringEncoding error:&error];
NSLog(#"JSon data = %# and Error = %#", json, error);
if(!error)
{
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSLog(#"JSON data is :: %#", myJsonArray);
for(NSDictionary *jsonDictionary in myJsonArray)
{
//NSString *uids = jsonDictionary[#"UID"];
NSString *address1 = jsonDictionary[#"Address1"];
NSString *address2 = jsonDictionary[#"Address2"];
NSString *city = jsonDictionary[#"City"];
NSString *emailId = jsonDictionary[#"EmailID"];
NSString *fname = jsonDictionary[#"FName"];
NSString *fax = jsonDictionary[#"Fax"];
NSString *lname = jsonDictionary[#"LName"];
NSString *password = jsonDictionary[#"Password"];
NSString *phone = jsonDictionary[#"Phone"];
NSString *state = jsonDictionary[#"State"];
NSString *uids = [jsonDictionary objectForKey:#"UID"];
NSString *zip = jsonDictionary[#"Zip"];
NSString *company = jsonDictionary[#"company"];
NSString *department = jsonDictionary[#"department"];
NSLog(#"Uid is = %#", uids);
NSLog(#"First Name = %#", fname );
NSLog(#"Last Name = %#", lname);
NSLog(#"Company = %#", company);
NSLog(#"Email Id = %#", emailId);
NSLog(#"Password = %#", password);
NSLog(#"Department = %#", department);
NSLog(#"Address 1 = %#", address1);
NSLog(#"Address 2 = %#", address2);
NSLog(#"City = %#", city);
NSLog(#"State = %#", state);
NSLog(#"Zip = %#", zip);
NSLog(#"Phone = %#", phone);
NSLog(#"Fax = %#", fax);
}
}
});
[activity stopAnimating];
self.activity.hidden = YES;
}
Image will give you where the error is. I get this error after clicking stepover to debug. I also tried
NSString *address1 = [jsonDictionary objectForKey:#"Address1"];
From the output of url, it shows it's not array but a dictionary. You are trying to convert to array here.
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
instead use this
NSDictionary *myJsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
then remove that for loop no need of it. and replace your variable jsonDictionary with myJsonDictionary so to retrieve values.
// for(NSDictionary *jsonDictionary in myJsonArray)
Run now it will be fine. Worked for me fine
If the output was array of Dictionaries it would have been looked like this with square brackets around.
For Ex: [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
If you are not sure of nature of response from url you can check for
the type. For ex:
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSLog(#"its an array!");
NSArray *myJsonArray = (NSArray *)jsonObject;
// Handle Array of Dictionary
for(NSDictionary *jsonDictionary in myJsonArray)
{
NSString *address1 = jsonDictionary[#"Address1"];
//and so on
}
}
else {
NSLog(#"It's Dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSLog(#"jsonDictionary - %#",jsonDictionary);
//Handle NSDictionary
}
jsonDictonary is a NSString not as you expect NSDictonary.
Double check your JSON and maybe before calling that function check if it's NSDictonary.
Your should use one of the semi-standard frameworks for parsing JSON into Core Data. There are some SO questions about those.
In this case, your JSON has only one object which is not an array.
In general, your app shouldn't abort if server sent something unexpected, so it's better to use a parser which will loudly complain about malformed JSON than with it from scratch.