I'm writing an app that deals with Albums of pictures.
我正在编写一个应用相册专辑的应用。
I have the following JSON document which lists Albums.
我有以下JSON文档列出了专辑。
{
"Albums": [
{
"id": 1234,
"Title": "My Birthday 2008",
"Category": {
"id": 0,
"Name": "Birthdays"
}
},
{
"id": 5678,
"Title": "Disneyland Trip 2010"
"Category": {
"id": 1,
"Name": "Trips"
},
}
]
}
As you can see, Albums reside in Categories.
如您所见,相册位于类别中。
I've used NSJSONSerialization class to bring this into an NSDictionary object which contains an NSArray of NSDictionaries (per the "schema" of the above JSON).
我已经使用NSJSONSerialization类将它带入一个NSDictionary对象,该对象包含NSDurray的NSArray(根据上述JSON的“模式”)。
From this, I want to generate an NSArray of unique Categories NSDictionary objects. Something like:
从这里,我想生成一个NSArray独特的类别NSDictionary对象。就像是:
{
"Categories": [
{
"id": 0,
"Name" : "Birthdays"
},
{
"id": 1,
"Name" : "Trips"
},
]
}
Scouring *, I've found some things that seem like good building blocks
淘汰*,我发现了一些看似很好的构建块的东西
categories = [albums valueForKeyPath:@"@distinctUnionOfObjects.Category.Name"];
but that obviously only generates an NSArray of NSStrings. What I want is it to be an NSArray of NSDictionaries which has two objects in it with keys "id" and "Name". I can't seem to put it all together to get what I want, without the poorman/brute method of nested if loops. I guess I'm looking for some utility method for the NSDictionary or NSArray classes that can help me do this faster.
但这显然只会产生NSStrings的NSArray。我想要的是它是NSDiction的NSArray,其中有两个对象,其中包含键“id”和“Name”。我似乎无法将所有内容放在一起以获得我想要的东西,而没有嵌套if循环的穷人/暴力方法。我想我正在寻找NSDictionary或NSArray类的一些实用工具方法,可以帮助我更快地完成这项工作。
Any help is appreciated, thanks.
任何帮助表示赞赏,谢谢。
1 个解决方案
#1
0
Why not just use the JSON parser? It should give you what you want.
为什么不使用JSON解析器?它应该给你你想要的东西。
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html
#1
0
Why not just use the JSON parser? It should give you what you want.
为什么不使用JSON解析器?它应该给你你想要的东西。
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html