需要帮助来解析复杂的JSON

时间:2011-03-23 15:38:06

标签: iphone objective-c json ios

我试图解析一个复杂的json(使用Json框架),问题是json有很多嵌套对象。花了太多时间编程,我找不到解决方案,我的代码闻起来,我有很多崩溃。 任何的想法? (我在使用其他json框架时没有任何问题,比如jsonkit或其他)

以下是json的简化版本。

[
{
    "category": {
        "id": 451,
        "description": "Product description",
        "quantity": "3149",
        "category": [
            {
                "category": {
                    "id": 115,
                    "description": "Product description",
                    "quantity": "465",
                    "category": [
                        {
                            "category": {
                                "id": 125,
                                "description": "Product description",
                                "quantity": "6"
                            }
                        },
                        {
                            "category": {
                                "id": 120,
                                "description": "Product description",
                                "quantity": "57",
                                "category": [
                                    {
                                        "category": {
                                            "id": 193,
                                            "description": "Product description",
                                            "quantity": "27"
                                        }
                                    },
                                    {
                                        "category": {
                                            "id": 194,
                                            "description": "Product description",
                                            "quantity": "14"
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "category": {
                                "id": 446,
                                "description": "Product description",
                                "quantity": "7"
                            }
                        },
                        {
                            "category": {
                                "id": 132,
                                "description": "Product description",
                                "quantity": "28",
                                "category": [
                                    {
                                        "category": {
                                            "id": 224,
                                            "description": "Product description",
                                            "quantity": "3"
                                        }
                                    },
                                    {
                                        "category": {
                                            "id": 137,
                                            "description": "Product description",
                                            "quantity": "4"
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "category": {
                                "id": 128,
                                "description": "Product description",
                                "quantity": "21"
                            }
                        }
                    ]
                }
            },
            {
                "category": {
                    "id": 21,
                    "description": "Product description",
                    "quantity": "225",
                    "category": [
                        {
                            "category": {
                                "id": 170,
                                "description": "Product description",
                                "quantity": "1"
                            }
                        },
                        {
                            "category": {
                                "id": 68,
                                "description": "Product description",
                                "quantity": "45",
                                "category": [
                                    {
                                        "category": {
                                            "id": 81,
                                            "description": "Product description",
                                            "quantity": "2"
                                        }
                                    },
                                    {
                                        "category": {
                                            "id": 69,
                                            "description": "Product description",
                                            "quantity": "2"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
},
{
    "category": {
        "id": 390,
        "description": "Product description",
        "quantity": "251",
        "category": [
            {
                "category": {
                    "id": 714,
                    "description": "Product description",
                    "quantity": "0"
                }
            },
            {
                "category": {
                    "id": 622,
                    "description": "Product description",
                    "quantity": "22",
                    "category": [
                        {
                            "category": {
                                "id": 192,
                                "description": "Product description",
                                "quantity": "16"
                            }
                        },
                        {
                            "category": {
                                "id": 391,
                                "description": "Product description",
                                "quantity": "6"
                            }
                        }
                    ]
                }
            },
            {
                "category": {
                    "id": 612,
                    "description": "Product description",
                    "quantity": "5"
                }
            },
            {
                "category": {
                    "id": 621,
                    "description": "Product description",
                    "quantity": "123",
                    "category": [
                        {
                            "category": {
                                "id": 628,
                                "description": "Product description",
                                "quantity": "25"
                            }
                        },
                        {
                            "category": {
                                "id": 457,
                                "description": "Product description",
                                "quantity": "3"
                            }
                        }
                    ]
                }
            },
            {
                "category": {
                    "id": 720,
                    "description": "Product description",
                    "quantity": "1"
                }
            },
            {
                "category": {
                    "id": 188,
                    "description": "Product description",
                    "quantity": "52",
                    "category": [
                        {
                            "category": {
                                "id": 437,
                                "description": "Product description",
                                "quantity": "10"
                            }
                        },
                        {
                            "category": {
                                "id": 639,
                                "description": "Product description",
                                "quantity": "7"
                            }
                        },
                        {
                            "category": {
                                "id": 152,
                                "description": "Product description",
                                "quantity": "4",
                                "category": [
                                    {
                                        "category": {
                                            "id": 34,
                                            "description": "Product description",
                                            "quantity": "3"
                                        }
                                    },
                                    {
                                        "category": {
                                            "id": 31,
                                            "description": "Product description",
                                            "quantity": "1"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            {
                "category": {
                    "id": 548,
                    "description": "Product description",
                    "quantity": "3"
                }
            }
        ]
    }
}]

我的目的是创建一个对象数组来填充数据库,表格如下:

idfather(父亲类别)
idproduct
说明
数量
时间戳

我尝试的最后一件事:

- (void)moreCategories:(NSString *)myString {
    NSLog(@"%@",myString);
    NSDictionary *mydict =[myString valueForKeyPath:@"category"] ;
    NSMutableArray *myArray = [[NSMutableArray alloc] init];

    myArray = [mydict valueForKey:@"category"] ;
    NSLog(@"count %d",[myArray count]);
    for (int i = 0; i < [myArray count]; i++)
    {
        NSDictionary *dict = [myArray objectAtIndex:i];
//sometime crashes here
        if (dict ){
            //NSLog(@"dict: %d",[dict count]);
            //NSLog(@"id %d",[[dict objectForKey:@"id"] intValue]);
            //NSLog(@"description %@",[dict objectForKey:@"description"] );
            //NSLog(@"quantity %d",[[dict objectForKey:@"quantity"] intValue]);
            if ([dict valueForKeyPath:@"category"]) {
                [self moreCategories:[dict valueForKeyPath:@"category"] ];

            }   
        }
    }

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding]autorelease];
    [responseData release];

    NSDictionary *results = [responseString JSONValue]; 
    self.allData = [results valueForKey:@"category"] ;
    NSLog(@"count %d",[self.allData count]);

    for (int i = 0; i < [self.allData count]; i++)
    {
        NSDictionary *dict = [self.allData objectAtIndex:i];
        //NSLog(@"id %d",[[dict objectForKey:@"id"] intValue]);
        NSLog(@"description %@",[dict objectForKey:@"description"] );
        //NSLog(@"quantity %d",[[dict objectForKey:@"quantity"] intValue]);
        if ([dict objectForKey:@"category"]) {
            //NSLog(@"%@",[dict valueForKeyPath:@"category"]);
            //NSLog(@"%@",[self.allData objectAtIndex:i]);
            [self moreCategories:[dict objectForKey:@"category"] ];
            //NSLog(@"%@",[self.allData objectAtIndex:i]);
        }   
    //  NSLog(@"%@",[self.allData objectAtIndex:i]);
    }   

}

2 个答案:

答案 0 :(得分:1)

你可以使用TouchJSON,它非常简单并且具有良好的性能。它将您的json作为数据,并且可以在一行中返回NSArray或NSDictionnary。

如果您的JSON具有列表结构:

NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:yourData error:nil];

否则:

NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:yourData error:nil;

您只需在项目中包含TouchJSON并在要使用它的类中导入#import "CJSONDeserializer.h"https://github.com/TouchCode/TouchJSON

答案 1 :(得分:0)

看起来您的问题可能是JSON数据结构中的列表。当我使用JSON框架时,我无法使用keypath访问数组值。

我解决了服务器端的问题,不允许数据结构中的数组 - 只有键/值对。然后我可以使用来自根节点的密钥路径访问结构中的每个值。 (当我想将列表转换为JSON时,我只使用了从0开始的数字键。)

您确定数据结构的语法是否正确?也许你的框架无法处理它 - 尽管它应该能够。我用过这个:https://github.com/stig/json-framework/