get values out of jSON Array - Objective C [duplicate] - ios
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);
Related
NSJSONSerialization not working with emoji [duplicate]
This question already has answers here: NSJSONSerialization and Emoji (2 answers) Closed 6 years ago. and i want to get values out of this. String "[{\"VisitorId\":\"dbd1c1d6-8348-4674-9e48-65137bdc5c14\",\"OperatorID\":null,\"VisitorSignalId\":\"6c454fc2-cf74-4010-aecd-93d93408ad24\",\"Type\":\"Visitor\",\"Messagetext\":\"ksksksksksk has joined the chat!\",\"IsJoin\":true,\"MessageTime\":\"2017-02-07 05:10:28\",\"File\":null,\"MessageType\":\"JoinMessage\",\"Name\":\"ksksksksksk\",\"Email\":\"skdsm#fdg.com\",\"IP\":\"123.231.110.188\",\"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\":\"dbd1c1d6-8348-4674-9e48-65137bdc5c14\",\"OperatorID\":null,\"VisitorSignalId\":null,\"Type\":\"Visitor\",\"Messagetext\":\"lkkhnl\",\"IsJoin\":false,\"MessageTime\":\"2017-02-07 05:10:58\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"ksksksksksk\",\"Email\":\"skdsm#fdg.com\",\"IP\":\"123.231.110.188\",\"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\":null,\"OperatorID\":\"81aebba9-ddc3-4f58-b1af-ac912d23a41e\",\"VisitorSignalId\":null,\"Type\":\"Operator\",\"Messagetext\":\"how can i help you today?\",\"IsJoin\":false,\"MessageTime\":\"2017-02-07 10:41:11\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"sathya\",\"Email\":null,\"IP\":null,\"Page\":null,\"Rating\":null,\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":\"sathya\",\"OperatorCount\":1,\"RequestTranscript\":null,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":\"dbd1c1d6-8348-4674-9e48-65137bdc5c14\",\"OperatorID\":null,\"VisitorSignalId\":\"6c454fc2-cf74-4010-aecd-93d93408ad24\",\"Type\":\"Visitor\",\"Messagetext\":\"Visitor is no longer active\",\"IsJoin\":false,\"MessageTime\":\"2/7/2017 5:11:53 AM\",\"File\":null,\"MessageType\":\"TransferMessage\",\"Name\":\"ksksksksksk\",\"Email\":\"skdsm#fdg.com\",\"IP\":\"123.231.110.188\",\"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\":null,\"OperatorID\":\"81aebba9-ddc3-4f58-b1af-ac912d23a41e\",\"VisitorSignalId\":null,\"Type\":\"Operator\",\"Messagetext\":\"Hi\",\"IsJoin\":false,\"MessageTime\":\"2017-02-07 10:47:54\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"sathya\",\"Email\":null,\"IP\":null,\"Page\":null,\"Rating\":null,\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":\"sathya\",\"OperatorCount\":1,\"RequestTranscript\":null,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":null,\"OperatorID\":\"81aebba9-ddc3-4f58-b1af-ac912d23a41e\",\"VisitorSignalId\":null,\"Type\":\"Operator\",\"Messagetext\":\"how can i help you today?\",\"IsJoin\":false,\"MessageTime\":\"2017-02-07 10:50:07\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"sathya\",\"Email\":null,\"IP\":null,\"Page\":null,\"Rating\":null,\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":\"sathya\",\"OperatorCount\":1,\"RequestTranscript\":null,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null},{\"VisitorId\":null,\"OperatorID\":\"81aebba9-ddc3-4f58-b1af-ac912d23a41e\",\"VisitorSignalId\":null,\"Type\":\"Operator\",\"Messagetext\":\"💕\",\"IsJoin\":false,\"MessageTime\":\"2017-02-07 10:57:13\",\"File\":null,\"MessageType\":\"NormalMessage\",\"Name\":\"sathya\",\"Email\":null,\"IP\":null,\"Page\":null,\"Rating\":null,\"Image\":null,\"CurrentOpImage\":null,\"FileUploadDetails\":null,\"FirstOperatorName\":\"sathya\",\"OperatorCount\":1,\"RequestTranscript\":null,\"Country\":null,\"OS\":null,\"Broswer\":null,\"Mobile\":null,\"iso_code\":null}]" Method + (void) showCurrentHistory:(NSString *) historyString { NSString* processed = [historyString stringByReplacingOccurrencesOfString:#"\\\"" withString:#"\""]; NSString* withotExtras = [[processed substringFromIndex:1] substringToIndex:[processed length] - 2]; NSData *data = [[NSData alloc] initWithBytes:[withotExtras UTF8String] length:withotExtras.length]; NSError *error; NSMutableArray* jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 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 *MessageType = [dic objectForKey:#"MessageType"]; 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(#"MessageType : %#" , MessageType); NSLog(#"MessageTime : %#" , MessageTime); NSLog(#"Image : %#" , Image); NSLog(#"FileUploadDetails : %#" , FileUploadDetails); NSLog(#"FirstOperatorName : %#" , FirstOperatorName); NSLog(#"Rating : %#" , Rating); NSLog(#"------------------------------------------------------------------------------"); MessageItemObject *item = [[MessageItemObject alloc] init]; [item setVisitorId:VisitorId]; [item setVisitorSignalId:VisitorSignalId]; [item setType:Type]; [item setMessagetext:Messagetext]; [item setMessageType:MessageType]; [item setMessageTime:MessageTime]; [item setImage:Image]; [item setFileUploadDetails:FileUploadDetails]; [item setFirstOperatorName:FirstOperatorName]; [item setRating:Rating]; if (messagesList.count == 0) { messagesList = [[NSMutableArray alloc] init]; [messagesList addObject:item]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:#"Adding new chatting" forKey:#"Adding new Chatting"]; [[NSNotificationCenter defaultCenter] postNotificationName:#"PassData" object:nil userInfo:dic]; } else { [messagesList addObject:item]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:#"Adding new chatting" forKey:#"Adding new Chatting"]; [[NSNotificationCenter defaultCenter] postNotificationName:#"PassData" object:nil userInfo:dic]; } } } This method works fine if there is no emoji, since this sting has a single 💕\ emoji , NSJSONSerialization is not working. Can some one help me to fix this issue.
The problem is you are hardcoding the JSON string and making changes on it, don't do make any changes with your JSON string and directly convert it to NSData. NSData *data = [historyString dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; NSMutableArray* jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; NSLog(#"Array - %#", jsonArray);
Try this. Convert string with emoji to string with unicode like below NSData *data = [strEmoji dataUsingEncoding:NSNonLossyASCIIStringEncoding]; NSString *unicodeString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; And if you want to show that emoji somewhere then you can convert back in to emoji. NSData *data = [unicodeString dataUsingEncoding:NSUTF8StringEncoding]; NSString *emojiString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
Json to iOS Encoding Error
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.
Getting error when parsing JSON iOS
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); }
Unrecognized selector sent to instance in JSON parsing
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.
iOS JSON Parsing error
I am trying to parse a json here but keep on getting errors for some reason. my code is NSString *string = [NSString stringWithFormat:#"http://api.wunderground.com/api/3c158b3b3cd6ce90/hourly/q/11758.json"];//, query]; NSString *string2 = [string stringByReplacingOccurrencesOfString:#" " withString:#"_"]; NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:string]]; NSError* error; NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSArray* latestLoans = [res objectForKey:#"forecast"]; //2 NSArray *tmp = [latestLoans valueForKey:#"temp"]; NSLog(#"temp: %#", tmp); and the error is temp: (null) Entity: line 2: parser error : Start tag expected, '<' not found { ^ I don't get this because the URL is real and gets a response, and I need help with this
What you are looking for, "forecast", does not exist in the response. Perhaps what you are looking for is "hourly_forecast". NSArray* latestLoans = [res objectForKey:#"hourly_forecast"]; //2 NSArray *tmp = [latestLoans valueForKey:#"temp"];