Printing plist file to NSLog - ios

I have a plist file I'm creating, but instead of writing to a file I would like to print out it's contents as a string to NSLog:
NSMutableDictionary* root = [NSMutableDictionary dictionaryWithCapacity:1];
NSMutableArray* puzzle = [NSMutableArray arrayWithCapacity: [self.puzzlePieces count] ];
root[#"puzzle"] = puzzle;
[self.puzzlePieces enumerateObjectsUsingBlock:^(KTPuzzlePiece* piece, NSUInteger idx, BOOL *stop) {
NSMutableDictionary* pieceInfo = [piece.pieceInfo mutableCopy];
pieceInfo[#"x"] = [NSNumber numberWithFloat: piece.position.x];
pieceInfo[#"y"] = [NSNumber numberWithFloat: piece.position.y];
[puzzle addObject:pieceInfo];
}];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:root
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
However, the following below outputs hex values
NSLog(#"Data: %#", data);

You're just missing one step. You need to convert your NSData to a string and you should be good to go:
NSData *data = [NSPropertyListSerialization
dataFromPropertyList:root
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#", string);

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

get values out of jSON Array - Objective C [duplicate]

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

Need To Add New Keys and Values Into Plist Using Objective C? [duplicate]

This question already has answers here:
How To Add New Keys and Values Into Plist Using Objective C?
(1 answer)
Save NSDictionary to plist
(3 answers)
Closed 7 years ago.
I have created JSON data store Into Plist. Now the problem is after JSON data storage, I need to add two set of keys into every array of dictionary items like below Image_2.The key name isParent - Boolean YES and isChild - Boolean YES with levels like mentioned below Image_2.
Now I have below structure of plsit datas Its perfectly working by below code.
I need to add two keys for outside of object subjectcount and inside of objectsubjectcount red marked datas.
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = JSON[#"response"];
NSArray *keys = [response allKeys];
NSMutableArray *objects = [NSMutableArray new];
for (NSString *key in keys) {
NSMutableDictionary *object = response[key];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"subject = %#",object[#"subject"]];
NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
NSInteger subjects = [object[#"subject"] integerValue];
if (subjects > 0) {
NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
[object setObject:Objects_Subjectcount forKey:#"Objects_Subjectcount"];
for (NSInteger i = 0; i < subjects; i++) {
[Objects_Subjectcount addObject:object];// object or anything you need
}
}
[objects addObject:object];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"File.plist"];
NSError *writeError = nil;
NSDictionary *finalDict = #{#"Objects": objects};
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData){
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(#"Error in saveData: %#", error);
}
NOTE : all the datas store by JSON but after storage need to add additional values by manually! Thats I am trying
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListMutable error:&writeError];
Retrieve data from plist like this.
NSMutableDictionary *plistDic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
Now suppose you want to update first array of dictionary then
NSMutableArray *array = [[NSMutableArray alloc]init:[plistDic objectAtIndex:0]]
You can do for loop if you want to change all array data...Right i am just using first object data..
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init:[array objectAtIncex:0]];
[dict addObject:"yourdata" forKey:"yourkey"];
[array replaceObject:dict atIndex:0];
[plistDic replaceObjectAtIndex:0 withObject:array];
And last
[plistDic writeToFile:plistPath atomically:YES];
The object from response[key] is immutable so it can't be modified as below:
[object setObject:Objects_Subjectcount forKey:#"Objects_Subjectcount"];
Making it mutable as below, it can be modified by any method add/remove/set.
NSMutableDictionary *object = [response[key] mutableCopy];
Hope using below modified block, you would see required changes.
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = JSON[#"response"];
NSArray *keys = [response allKeys];
NSMutableArray *objects = [NSMutableArray new];
for (NSString *key in keys) {
NSMutableDictionary *object = [response[key] mutableCopy];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"subject = %#",object[#"subject"]];
NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
NSInteger subjects = [object[#"subject"] integerValue];
if (subjects > 0) {
[object setObject:#"" forKey:#"level"];
[object setObject:#(YES) forKey:#"isParent"];
NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
for (NSInteger i = 0; i < subjects; i++) {
[Objects_Subjectcount addObject:#{#"level":#(0), #"isChild":#(YES)}];
}
[object setObject:Objects_Subjectcount forKey:#"Objects_Subjectcount"];
}
[objects addObject:object];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"File.plist"];
NSError *writeError = nil;
NSDictionary *finalDict = #{#"Objects": objects};
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData){
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(#"Error in saveData: %#", error);
}

Add Additional Keys and Values Within PropertyList Using iOS

I need to add below mentioned keys and values (Image Red Marked Values) Into propertylist.
Needed Structure (need to add Red marked keys and values at same position)
Now my plist:
My Exact Scenario Below
I have created plist and Into the plist I am getting the storage
data from JSON parser.
I have one standard mediatory storage structure based on that Its
storing (I have added above Image two)
Now I need to add two boolean keys and values by
manual, Into the plist. Where and which position all the information
I have marked and mentioned by above Image one.
JSON parse to store data Into plist for that I have used below code
Below code I am using:
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = JSON[#"response"];
NSArray *keys = [response allKeys];
NSMutableArray *objects = [NSMutableArray new];
for (NSString *key in keys) {
NSMutableDictionary *object = response[key];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"subject = %#",object[#"subject"]];
NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
NSInteger subjects = [object[#"subject"] integerValue];
if (subjects > 0) {
NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
[object setObject:Objects_Subjectcount forKey:#"Objects_Subjectcount"];
for (NSInteger i = 0; i < subjects; i++) {
[Objects_Subjectcount addObject:object];// object or anything you need
}
}
[objects addObject:object];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"File.plist"];
NSError *writeError = nil;
NSDictionary *finalDict = #{#"Objects": objects};
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData){
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(#"Error in saveData: %#", error);
}
Assume your existing plist is created by this code:
NSDictionary *dic = #{#"Planet":#"Earth",
#"Earth":#{#"1":#"Asia",
#"2":#"Africa"}
};
[dic writeToFile:path atomically:YES];
Then you need to insert a new key-value pair inside Earth.
//read
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:path];
//create a mutable dict to edit it
NSMutableDictionary *mDict = [readDict mutableCopy];
//read the key-value for earth, and we are gonna edit it
NSMutableDictionary *earthDict = mDict[#"Earth"];
//set new object, it can be anything, string, array, dictionary etc
[earthDict setObject:#{#"myName":#"Anoop"}
forKey:#"new"];
//write it back to same file
[mDict writeToFile:path atomically:YES];
Now the plist looks like this:

How to Read Plist Data Using Objective C?

I have stored JSON data Into within Plist. Whenever I want to see my stored data I am taking path details and find into my machine then only I can see. I need to read data after storage using objective C.
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:&error];
NSDictionary *response = JSON[#"response"];
NSArray *keys = [response allKeys];
NSMutableArray *objects = [NSMutableArray new];
for (NSString *key in keys) {
NSMutableDictionary *object = response[key];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"subject = %#",object[#"subject"]];
NSArray *objectsWithSameSubject = [objects filteredArrayUsingPredicate:predicate];
NSInteger subjects = [object[#"subject"] integerValue];
if (subjects > 0) {
NSMutableArray *Objects_Subjectcount = [NSMutableArray new];
[object setObject:Objects_Subjectcount forKey:#"Objects_Subjectcount"];
for (NSInteger i = 0; i < subjects; i++) {
[Objects_Subjectcount addObject:object];// object or anything you need
}
}
[objects addObject:object];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"File.plist"];
NSError *writeError = nil;
NSDictionary *finalDict = #{#"Objects": objects};
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:finalDict format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData){
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(#"Error in saveData: %#", error);
}
Once you have the right path to the plist file, you can read it by:
NSMutableDictionary *plistDic = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int value = [[plistDic objectForKey:#”value”] intValue]; //to get a int value for value for example
and to write to plist:
[plistDic setObject:[NSNumber numberWithInt:2] forKey:#”value”];
[plistDic writeToFile: path atomically:YES];
Try this:
NSError *error = nil;
NSData * tempData = [[NSData alloc] initWithContentsOfFile:#"File.plist"];
NSPropertyListFormat plistFormat = nil;
NSDictionary *temp = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NSPropertyListXMLFormat_v1_0 error:&error];
Note :
when you read data using NSPropertyListSerialization you dont have to pass NSPropertyListFormat . you either pass nil or pass the address for NSPropertyListFormat.
Hope this will help.

Resources