【发布时间】:2012-08-23 15:31:59
【问题描述】:
我在使用 NSJSONSerialization 从 PHP 服务器解析 JSON 时遇到问题。 JSLint 说我的 JSON 是有效的,但似乎只能进入一两个级别。
这本质上是我的 JSON 结构:
{
"products":
[{
"product-name":
{
"product-sets":
[{
"set-3":
{
"test1":"test2",
"test3":"test4"
},
"set-4":
{
"test5":"test6",
"test7":"test8"
}
}]
},
"product-name-2":
{
"product-sets":
[{
}]
}
}]
}
这是我的解析代码:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if (json) {
NSArray *products = [json objectForKey:@"products"]; // works
for (NSDictionary *pItem in products) { // works
NSLog(@"Product: %@", pItem); // works, prints the entire structure under "product-name"
NSArray *productSets = [pItem objectForKey:@"product-sets"]; // gets nil
for (NSDictionary *psItem in productSets) {
// never happens
}
}
}
我已经为此旋转了几个小时,但在我搜索的任何地方都找不到类似的东西。是否有任何我不知道的限制,或者我只是没有看到明显的东西?
【问题讨论】:
-
对我来说产品集似乎是空的。没有项目,没有迭代
-
这是有道理的,但是我在产品集中添加了一些值,但我仍然遇到同样的问题。我将编辑问题以包含此内容。谢谢!
标签: objective-c ios json parsing nsjsonserialization