-3

I have a little Problem with JSON to NSDictionary, and hope someone can help me:

From an URL-Request i get JSON Objects like this:

{
    page = 1;
    results = (
        {
            adult = 0;
            "vote_average" = 5;
            "vote_count" = 100;
        },
        {
            adult = 0;
            "vote_average" = 3;
            "vote_count" = 10;
        }, 
        {
            adult = 0;
            "vote_average" = 1;
            "vote_count" = 300;
        },
    );
    "total_pages" = 1;
    "total_results" = 3;
}

Now I need to parse this JSON Objects into NSDictionary. My problem is that there are more (this time 3) JSON-Objects and each Object has same keys. I think i need to create a NSDictionary for each JSON Object, do I? ..is there a simple way to do this? Or is there another "usual way" to handle such a situation?

Thanks fore Help!

4

2 回答 2

0

那是一个字典,结果的键有一个值,如果字典是一个数组。NSJSONSerialization 可以解决这个问题。

于 2013-06-16T18:13:09.463 回答
0

您不必单独创建所有对象,NSJSONSerialization 可以为您完成这项工作。不断变化的项目数量来自数组内部,因此数组中的对象数量会增加,仅此而已

利用

  NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

data 是转换为 NSData 的 json 字符串

编辑:那么 json 到字典和数组的结构非常简单, {表明它是一个字典,(或者[表明它是一个数组,如果它处于嵌套状态,是的,字典也是同样的方式

所以在你的情况下,你必须填充数组并在数组索引中获取对象,它将有一个字典,你可以在那里实现一个值

所以在你的情况下

    NSArray *results=[jsonResultDictionary objectForKey:@"results"];
    NSDictionary *dict=[results objectAtIndex:0];
    NSLog(@"%@",[dict objectForKey:@"vote_average"]);
于 2013-06-16T18:15:53.073 回答