0

重複の可能性:
Objective-C で JSON を解析するにはどうすればよいですか?

まず、Objective-C はまだ初心者ですが、JSON データを RESTful API から変数に保存する方法を探しています。API への接続は既に確立していますが、特定の情報を検索するためのコマンドに慣れていません。ヘルプやリファレンスをいただければ幸いです。ありがとう!

4

3 に答える 3

1

Objective-cのJSONパーサーを追加します。そのオープンソース。

SBJSON *parser = [[SBJSON alloc] init];
NSString *tempStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

// parse the JSON string into an object - assuming json_string is a NSString of JSON data
id object = [parser objectWithString:tempStr error:nil];

[resultObj release];
resultObj = [object retain];
于 2013-01-21T07:04:50.153 に答える
1

RESTful API から JSON データを取得する最も簡単な方法の 1 つは、AFNetworkingライブラリを使用することです。

これがサンプルコードです

NSURL *url = [NSURL URLWithString:@"http://yourAPIUrl/whatever"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    // do whatever you like with your JSON object
    NSLog(JSON);
} failure:nil];

[operation start];
于 2013-01-21T05:36:08.880 に答える
-1
NSDictionary* myDict =  [NSJSONSerialization JSONObjectWithData:myJsonData 
                                                        options:0 
                                                          error:&error];
于 2013-01-21T03:28:05.553 に答える