-1

我正在使用 SZJsonParser 从站点下载 JSON 对象。这是我的代码:

#import <Foundation/Foundation.h>
#import "SZJsonParser.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {

        // insert code here...
        NSURL *url = [[NSURL alloc] initWithString:@"http://mymovieapi.com/?title=Cars"];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSArray *arr = [str jsonObject];
        for (int i = 0; i < [arr count]; i ++)
        {
            NSLog(@"%@",[arr objectAtIndex:i]);
        }
    }
    return 0;
}

当我运行终端应用程序时,输出如下:

2013-09-19 15:54:49.957 SimpleJSONTerminal[24160:303] {
    actors =     (
        "Owen Wilson",
        "Paul Newman",
        "Bonnie Hunt",
        "Larry the Cable Guy",
        "Cheech Marin",
        "Tony Shalhoub",
        "Guido Quaroni",
        "Jenifer Lewis",
        "Paul Dooley",
        "Michael Wallis",
        "George Carlin",
        "Katherine Helmond",
        "John Ratzenberger",
        "Joe Ranft",
        "Michael Keaton"
    );
    "also_known_as" =     (
        "Cars - Quatre roues"
    );
    country =     (
        USA
    );
    directors =     (
        "John Lasseter",
        "Joe Ranft"
    );
    genres =     (
        Animation,
        Adventure,
        Comedy,
        Family,
        Sport
    );
    "imdb_id" = tt0317219;
    "imdb_url" = "http://www.imdb.com/title/tt0317219/";
    language =     (
        English,
        Italian,
        Japanese,
        Yiddish
    );
    "plot_simple" = "A hot-shot race-car named Lightning McQueen gets waylaid in Radiator Springs, where he finds the true meaning of friendship and family.";
    poster =     {
        cover = "http://imdb-poster.b0.upaiyun.com/000/317/219.jpg!cover?_upt=65f114a91379629498";
        imdb = "http://ia.media-imdb.com/images/M/MV5BMTE5Mzk5MTA2Ml5BMl5BanBnXkFtZTYwNTY3NTc2._V1_SY317_CR0,0,214,317_.jpg";
    };
    rating = "7.3";
    "rating_count" = 150431;
    "release_date" = 20060822;
    runtime =     (
        "117 min"
    );
    title = "Cars - Quatre roues\n            \"Cars\"";
    type = VG;
    writers =     (
        "John Lasseter",
        "Joe Ranft"
    );
    year = 2006;
}

如何将其存储在 NSDictionary 中,以便我可以使用“标题”作为键,然后获取信息?

谢谢

4

3 回答 3

0
Usethis........ 


NSURL *url = [[NSURL alloc] initWithString:@"http://mymovieapi.com/?title=Cars"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *dict = [NSPropertyListSerialization
                          propertyListWithData:[str dataUsingEncoding:NSUTF8StringEncoding]
                          options:kNilOptions
                          format:NULL
                          error:NULL];
    NSLog(@"dict:%@",dict);
于 2013-09-19T10:47:28.037 回答
0
NSData *jsonData = [content dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *result = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err] : nil;

if(err)
{

            NSLog(@"JSON Failed. Error - %@ %@",
                 [err localizedDescription],
                 [[err userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
else
{
    // Do what you have to do with your data
}
于 2013-09-19T10:35:47.440 回答
0
NSData *theData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:jsonFileName ofType:@"json"]];

NSDictionary *myDictionary = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];

希望这会帮助你。

于 2013-09-19T10:29:33.303 回答