-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
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 に答える
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 に答える