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];
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);