php - 不解析 PHP JSON

标签 php ios objective-c json

我不确定我的 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/

相关文章:

ios - SpriteKit : add UIGestureRecognizer and detect which node was swiped

php - 对某些字段使用按字段排序,但在日期字段中保持顺序 - MySQL PHP

javascript - 使用另一个属性代替输入值来更新总计

java - 如何从 Java 连接到 SOAP 服务器并运行我的服务器内置方法/服务?

objective-c - NSOutlineView 和几个模型

ios - UIRefreshControl - 在 iOS 7 中拉动刷新

iphone - 获取当前 iTunesStore 国家

php - 使用 php 和 jquery 将输入从表单插入到数据库

iOS Horizo​​ntal CollectionView 没有一直滚动

ios - 如何使用计时功能缓慢加速 SpriteKit 的缩放功能