我不确定我的 JSON 或我在 xcode 中的代码是否有问题,但我无法让 xcode 解析我的 JSON。我的 JSON 有效,但我无法解析它。有人看到我做错了什么吗?
这是我用于生成 JSON 的 PHP:
$channel['items']['post']['category_name'][$category_name]['details'][] = array(
'title' => $title,
'description' => $description,
'category_id' => $category_id,
);
}
$channels = array($channel);
$json = json_encode($channel);
header('Content-type: application/json');
echo $json;
它给出了这样的 JSON:
{
"items": {
"post": {
"category_name": {
"Baby": {
"details": [
{
"title": "trying with category id again",
"price": "3344.55",
},
{
"title": "this is sending with description ",
"price": "900000.00",
}
]
},
“Other Baby Stuff": {
"details": [
{
"title": "putting in title",
"price": "3000.99",
这是我在 xcode 中要解析的代码(我希望我的部分标题是类别名称,如“Baby”、“Other Baby Stuff”等:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[m_tableView reloadData];
});
_buyCategory = json[@"items"][@"post"][@"category_name"];//THIS IS PARSING JUST FINE NOW
NSLog(@"Items: %@", _buyCategory);
NSLog(@"Array Count: %u", [_buyCategory count]);
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (appDelegate.bLoadBuy == 1) {
NSString *section = [[_buyCategory allKeys] objectAtIndex:0]; ///NOT RETURNING ANYTHING
NSLog(@"category: %@", section); //I AM NOT EVEN GETTING NSLOG HERE
return [NSString stringWithFormat:@"%@",section];];
在我的表格 View 单元格中(我想要我的详细信息)
_buyTitle = [[tempResults objectForKey:@"details"] objectForKey:@"title"];
最佳答案
NSDictionary *category = json[@"items"][@"post"][@"category_name"];
//section title
NSString *section = [[category allKeys] objectAtIndex:0]
//array of detail object.. so details[0][@"title"] should give you the first titme
NSArray *details = [category objectForKey:section];
关于php - 不解析 PHP JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165522/