如何使用目标C将JSON解析的数据存储到Plist中? [英] How To Store JSON Parsed Data Into Plist Using Objective C?
问题描述
我需要将JSON
解析的数据存储到propertylist
中,就像使用objective C
的以下结构一样.
I need to store JSON
parsed data Into propertylist
like below structure using objective C
.
我的服务器JSON响应:
My JSON Response From Server :
{
"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
.进入此属性列表Item 0 , 1, 2
,其名为JSON响应A, B, C
的值应作为字典和数组.
I am trying like below storage method into propertylist
. Into this propertylist Item 0 , 1, 2
Its called JSON response A, B, C
values Into Array and Item should as a Dictionary.
NOTE :
A, B, C
的值将基于JSON响应而增加,我的意思是它不是静态的,它可能会升至Z.
NOTE :
The A, B, C
values will Increase based on JSON response , I mean Its not static It will go upto Z may be.
此Array
值首先应将每个值存储为dictionary
.在该词典中需要添加如下字符串值,如果基于该计数,如果主题计数大于0,则应创建词典数组,例如在Image下方.
Into this Array
values first thing every values should store as a dictionary
. Within that dictionary need to add string values like below and If subject count above 0 based on that count It should create Array of dictionary like below Image.
例如:
-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);
}
如果我对您问题的第二部分的理解是正确的,则代码将如下所示:
If my understanding of your question's second part is correct, the code will be like this
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
这篇关于如何使用目标C将JSON解析的数据存储到Plist中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!