需要帮助来解析复杂的 JSON

Posted

技术标签:

【中文标题】需要帮助来解析复杂的 JSON【英文标题】:Need help to parse a complex JSON 【发布时间】:2011-03-23 15:38:06 【问题描述】:

我正在尝试解析一个复杂的 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]);
       


【问题讨论】:

你没有详细告诉我们你的问题是什么。哪里崩溃了?是什么原因?从 json-framework 返回后处理这些信息的代码是什么样的?所有这些都是需要回答的有效问题,然后才能有人给你任何可靠的建议。 那个 JSON 样本并不过分复杂。任何 JSON 库都应该能够处理这个问题。所以问题不在于解析 JSON,问题可能在于您遍历结果对象。但如果没有一些代码,我们永远不会知道。 键值编码呢?它基本上解决了我所有关于遍历 JSON 数据的问题。 您应该检查每一步返回的对象的类型。 (例如使用isKindOfClass。)这将帮助您更快地发现对数据结构的误解。 moreCategories 的参数不是NSString*。您应该将正式类型更改为其他类型。 【参考方案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

【讨论】:

【参考方案2】:

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

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

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

【讨论】:

以上是关于需要帮助来解析复杂的 JSON的主要内容,如果未能解决你的问题,请参考以下文章

我需要使用 Alamofire 解析 Json 的帮助

需要 SQL 查询帮助以解析 BigQuery 表中的 JSON 数据

带有 JSON 的 UIPickerView

6-4 JSON解析与复杂模型转换实用技巧

Json解析

解析 JSON 时出错