开发者

Need help to parse a complex JSON

I´m trying to parse a complex json (using the Json framework), the problem is that the json has a lot of nested objects. Took too much time programming and i can´t find the solution, my code smells and i have a lot of crashes. Any idea? (i don´t have any problem on using another json framework, like jsonkit or another)

Here is a simplified version of the 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"
                }
            }
        ]
    }
}]

My intention is create an array of objects to populate the database, the table looks like this:

idfather (the father category)

idproduct

description

quantity

timestamp

The last thing i tried:

- (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]);
    }   

}


You can use TouchJSON, it's really simple and have good performances. It takes your json as data and can return you a NSArray or an NSDictionnary in one line.

If your JSON have a list structure:

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

Else:

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

You just have to include TouchJSON in your project ans import #import "CJSONDeserializer.h" in the classes you want to use it. https://github.com/TouchCode/TouchJSON


Looks like your problem might be the list in the JSON data structure. When I used the JSON framework, I was not able to access array values with keypaths.

I solved the problem on the server side, not allowing arrays in the data structure - only key/value pairs. Then I could access every value in the structure with a keypath from the root node. (I just used numbers up from 0 for the keys when I wanted to transform a list to JSON.)

And are you sure the syntax of the data structure is correct? Maybe your framework can't handle it - although it should be able to. I used this one: https://github.com/stig/json-framework/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜