xcode 将 JSon 数据保存在 NSArray 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9868096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save JSon data in NSArray
提问by aur0n
I'm trying to save some specific JSon data and store them in a simple NSArray.
我正在尝试保存一些特定的 JSon 数据并将它们存储在一个简单的 NSArray 中。
This is the JSon I have:
这是我拥有的 JSon:
products = {
72 = {
value_a = "something1";
value_b = "something2";
value_c = "something3";
};
73 = {
value_a = "something1";
value_b = "something2";
value_c = "something3";
};
74 = {
value_a = "something1";
value_b = "something2";
value_c = "something3";
};
[etc]
}
This is how I assign the JSon data in a NSMutableArray.
这就是我在 NSMutableArray 中分配 JSon 数据的方式。
NSMutableArray *array = [json objectForKey:@"products"];
What I need is to store, let's say, just value_b of each child in an array. Any suggestion?
我需要的是将每个孩子的 value_b 存储在一个数组中。有什么建议吗?
回答by Alladinian
What you have here is a dictionary containing key/values, where values are also dictionaries. If you need to have an array containing, say value_b, of each of these dictionaries you can have it like this:
您在这里拥有的是一个包含键/值的字典,其中值也是字典。如果你需要一个包含每个字典的数组,比如 value_b,你可以这样使用它:
NSDictionary *products = [json objectForKey:@"products"];
NSMutableArray *b_values = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *product in [products allValues])
{
[b_values addObject:[product valueForKey:@"value_b"]];
}
// Now you have an array with all "value_b" objects
回答by hp iOS Coder
I dont know what you are using, But I can suggest you to use SBJsonFramework
我不知道你在使用什么,但我可以建议你使用SBJsonFramework
This kit has very convenient methods to store json data. Store all Json Data in
该套件具有非常方便的方法来存储 json 数据。将所有 Json 数据存储在
NSMutableDictionary
NSMutableDictionary
Then for each object you can extract specific value using key to store them in
然后对于每个对象,您可以使用键提取特定值将它们存储在
NSMutableArray
可变数组