Ios 如何使用Objective C将JSON解析数据存储到Plist中?
我需要使用Ios 如何使用Objective C将JSON解析数据存储到Plist中?,ios,objective-c,json,dictionary,plist,Ios,Objective C,Json,Dictionary,Plist,我需要使用objective C将JSON解析的数据存储到propertylist中,就像下面的结构一样 来自服务器的我的JSON响应: { "response":{ "A":{ "name":"Arun", "age":"20", "city":"SFO", "subject":2 }, "B":{ "name":"Benny", "ag
objective C
将JSON
解析的数据存储到propertylist
中,就像下面的结构一样
来自服务器的我的JSON响应:
{
"response":{
"A":{
"name":"Arun",
"age":"20",
"city":"SFO",
"subject":2
},
"B":{
"name":"Benny",
"age":"20",
"city":"SFO",
"subject":1
},
"C":{
"name":"Nani",
"age":"30",
"city":"SFO",
"subject":0
}
},
"inprogressdata":{
},
"dataspeed":"112 milliseconds..."
}
我正在尝试将下面的存储方法放入propertylist
。将其调用的JSON响应A、B、C
值放入数组,并将该项作为字典
注意:
根据JSON响应,A、B、C
值将增加,我的意思是它不是静态的,它可能会上升到Z
在这个数组中
值首先是每个值都应该存储为字典
。在该字典中,需要添加如下所示的字符串值,如果主题计数高于0,则应基于该计数创建如下图所示的字典数组
例如:
-Root
|
|-----Objects [Array Value]
|
|------Item 0(A) [Dictionary Value]
|
|------name (String)
|------age (String)
|------city (String)
|------subject (Number) // If number 2 then need to create "Objects_Subjectcount" [Array Values]
|------Objects_Subjectcount (Array)
|
|------Item 0 [Dictionary Value]
|------Item 1 [Dictionary Value]
试试这个代码
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary *response = JSON[@"response"];
NSArray *objects = [response allValues];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"File.plist"];
NSError *writeError = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:objects format:NSPropertyListXMLFormat_v1_0 options:NSPropertyListImmutable error:&writeError];
if(plistData){
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(@"Error in saveData: %@", error);
}
如果我对你问题第二部分的理解是正确的,代码将是这样的
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);
}
请阅读这篇文章,以获取您的答案。这没有帮助,因为我已经完成了plist创建和存储retiveal过程。我需要像上面的structure ex:dictionary数组那样存储数据,然后根据信号计数再次创建dictionary nexted表单数组@iOS Learner查看以下链接:和。阅读这些内容,并根据需要编写代码到plist中。@Nikhil84这一切都是关于plist的,这不重要。我需要如何存储数据像上面的结构!我很好奇为什么需要使用
NSPropertyListSerialization
,而不是直接在plistDict
上调用-writeToFile:atomicaly:
。然后我发现这篇文章解释了NSPropertyListSerialization
的优点:这是一个很好的答案,顺便问一下,我如何查看存储的数据?这将在plistPath中。如果您在模拟器中运行,请打印(nslog)plistPath,并在finder中按shift+cmd+G,然后通过此路径并返回!它可以自动存储所有东西!哇,太棒了@JohnyKutty!顺便说一句,我问了另一个基于主题计数的问题需要添加嵌套的字典项数组@johnykuttyy你的意思是如果dict中有更多的项目具有相同的主题,那么它不应该添加到根目录中,而应该添加到同一个字典中,并使用键对象\u Subjectcount?
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);
}
1. Create JSON with mutable containers and leaves.
2. Iterate through all keys and values.
3. Check whether the subject value of object is greater than 0 in the existing subjects array? if yes add the object to
Objects_Subjectcount. Add the object to objects array.(Don't forget
to set an NSMutable array with key Objects_Subjectcount) to the
object. 4 . Make final dictionary to be written as plist - wrap the
objets array in a dictionary with key "objects"
5. Convert the dictionary to plist data and write it