ios - 如何使用 Objective C 将 JSON 解析的数据存储到 Plist 中?
<p><p>我需要使用 <code>objective C</code> 将 <code>JSON</code> 解析的数据存储到 <code>propertylist</code> 中,如下结构。</p>
<p>我的来自服务器的 JSON 响应:</p>
<pre><code>{
"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..."
}
</code></pre>
<p>我正在尝试将以下存储方法放入 <code>propertylist</code>。进入这个属性列表 <code>Item 0 , 1, 2</code> 它称为 JSON 响应 <code>A, B, C</code> 值 Into Array 和 Item 应该作为字典。 </p>
<p><code>注意:</code> <code>A, B, C</code> 值将根据 JSON 响应增加,我的意思是它不是静态的,它可能会上升到 Z。</p>
<p> <a href="/image/cFVCa.png" rel="noreferrer noopener nofollow"><img src="/image/cFVCa.png" alt="enter image description here"/></a> </p>
<p>在这个<code>Array</code> 值中,每个值首先应该存储为一个<code>dictionary</code>。在该字典中需要添加如下字符串值,如果基于该计数的主题计数高于 0 它应该创建字典数组,如下图所示。</p>
<p> <a href="/image/0qNXd.png" rel="noreferrer noopener nofollow"><img src="/image/0qNXd.png" alt="enter image description here"/></a> </p>
<p>例如:</p>
<pre><code>-Root
|
|-----Objects
|
|------Item 0(A)
|
|------name (String)
|------age (String)
|------city (String)
|------subject (Number) // If number 2 then need to create "Objects_Subjectcount"
|------Objects_Subjectcount (Array)
|
|------Item 0
|------Item 1
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个代码</p>
<pre><code>NSDictionary *JSON = ;
NSDictionary *response = JSON[@"response"];
NSArray *objects = ;
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = ;
NSError *writeError = nil;
NSData *plistData = ;
if(plistData){
;
}
else {
NSLog(@"Error in saveData: %@", error);
}
</code></pre>
<p>如果我对您问题第二部分的理解是正确的,代码将是这样的</p>
<pre><code>NSDictionary *JSON = ;
NSDictionary *response = JSON[@"response"];
NSArray *keys = ;
NSMutableArray *objects = ;
for (NSString *key in keys) {
NSMutableDictionary *object = response;
NSPredicate *predicate = ];
NSArray *objectsWithSameSubject = ;
NSInteger subjects = integerValue];
if (subjects > 0) {
NSMutableArray *Objects_Subjectcount = ;
;
for (NSInteger i = 0; i < subjects; i++) {
;// object or anything you need
}
}
;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = paths.firstObject;
NSString *plistPath = ;
NSError *writeError = nil;
NSDictionary *finalDict = @{@"Objects": objects};
NSData *plistData = ;
if(plistData){
;
}
else {
NSLog(@"Error in saveData: %@", error);
}
</code></pre>
<p>步骤是,</p>
<pre><code>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
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使用 Objective C 将 JSON 解析的数据存储到 Plist 中?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34488990/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34488990/
</a>
</p>
页:
[1]