从 NSDictionary 保存数据到 NSDictionary
Posted
技术标签:
【中文标题】从 NSDictionary 保存数据到 NSDictionary【英文标题】:Saving data in NSDictionary from NSDictionary 【发布时间】:2016-11-16 20:27:23 【问题描述】:这是我已解析并保存在名为 NSDictionary *dic 的字典中的 JSON 格式数据。我有另一个名为 dic1 的 NSDicitonary,我只想将数据从 //1 存储到 //3 我该怎么做那?请帮忙?
//0
[
"about": "Mother, actor, entrepreneur, fitness enthusiast and an eternal positive thinker",
"id": "238bb4ca-606d-4817-afad-78bee2898264",
"username": "Shilpa shetty kundr"
//1
,
"about": "Www.Nargisfakhri.com Instagram- @NargisFakhri Twitter- @NargisFakhri FaceBook- Nargis Fakhri ",
"id": "cda99c24-955a-4c58-a6a8-c811938df530",
"username": "Nargis Fakhri"
//2
,
"description": "Celebrating Black Friday in this lovely black cut out romper and floral accessories. I love black and i think it is the easiest thing to wear when i am in doubt. So a black romper solves my dilemma of what to wear when i am short of time to decide and outfit. When you wear black add some fun accessories to keep the outfit fun and lively.",
"id": "fa6d9bdf-eae3-4d6c-a668-9be94cfaf980",
"verb": "created this story on 25 October"
,
//3
"description": "Today's outfit! #ootd Sheer shirts are always so sexy. Team it up with a bustier inside and it looks elegant and sexy at the same time. I wore my purple sheer shirt with an embellished maxi skirt and statement necklace. Sheer shirts looks fabulous with statement necklaces or layered chains.",
"id": "066f2bac-1fa8-44f8-b2b6-780df3324c71",
"verb": "created this story on 21 October"
]
这就是我做的。?
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id allKeys = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
userIDArray=[[NSMutableArray alloc]init];
for (int i=0; i<[allKeys count]; i++)
NSDictionary *arrayResult = [allKeys objectAtIndex:i];
if([arrayResult objectForKey:@"id"]!=NULL)
NSString *id=[arrayResult objectForKey:@"id"];
[userIDArray addObject:id];
我的观点是在 allKeys 中保存整个数据,而不是将整个数据从 //0 保存到 //3 我只需要从 //02 到 //03 的数据存储在某个字典中。
【问题讨论】:
这是一个数组,不是字典 @dan sir 有什么解决办法吗? 目前还不清楚您真正想要做什么。您应该使用一些相关代码更新您的问题。显示您拥有的词典以及您希望如何从其中获取数据并将其放入另一个词典。 @HarshitGoel:写代码怎么样?浏览 NSArray 和 NSMutableArray 文档,看看有哪些方法。 @gasher729 先生,请查看更新。我已经编辑了材料。 【参考方案1】:假设你意识到这是数组:
NSArray *a = [[NSArray alloc] init];
NSMutableArray *a1 = [[NSMutableArray alloc] init];
for(int i = 1; i < a.count; i++)
[a1 addObject: a[i]];
【讨论】:
或者[subarrayWithRange]更整洁以上是关于从 NSDictionary 保存数据到 NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章
如何返回数据类型:Swift 中的 NSDictionary