1

API に URL リクエストを作成していますが、JSON をレンダリングする方法がわかりません。このような複数のユーザーの配列が生成され[{"user": "value"}, {"user":"value"}]、TableView を使用しようとしていたので、NSDictionary が必要ですが、JSON をレンダリングする方が良いと思いますのように{users: [{"user": "value"}, {"user":"value"}]}。リクエストを行うためのこのコードがあります

#import "JSONKit.h"
NSError *error = nil;
NSURLResponse *response = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost:3000/getusers"]];
[request setHTTPMethod:@"GET"];
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
users = [[jsonData objectFromJSONData] objectForKey:@"users"];
usersKeys = [users allKeys];

しかし、私はこのエラーが発生しています

2012-09-16 18:51:11.360 tableview[2979:c07] -[JKArray allKeys]: インスタンス 0x6d30180 に送信された認識されないセレクター 2012-09-16 18:51:11.362 tableview[2979: c07 ]例外 'NSInvalidArgumentException'、理由: '-[JKArray allKeys]: 認識されないセレクターがインスタンス 0x6d30180 に送信されました'

これを達成する方法が本当にわからないので、どんな助けも役に立ちます、ありがとう

4

2 に答える 2

2

このエラーが発生するのは、" jsonData" から解析されたものは必ずしも期待したもの (つまり、辞書) とは限らないためです。

おそらく、そのコードでエラーチェックが必要になるでしょう。

例えば:

NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(jsonData)
{
    id objectReturnedFromJSON = [jsonData objectFromJSONData];
    if(objectReturnedFromJSON)
    {
        if([objectReturnedFromJSON isKindOfClass:[NSDictonary class]])
        {
            NSDictionary * dictionaryFromJSON = (NSDictionary *)objectReturnedFromJSON;
            // assuming you declared "users" & "usersKeys" in your interface,
            // or somewhere else in this method
            users = [dictionaryFromJSON objectForKey:@"users"];
            if(users)
            {
                usersKeys = [users allKeys];
            } else {
                NSLog( @"no users in the json data");
            }
        } else {
            NSLog( @"no dictionary from the data returned by the server... check the data to see if it's valid JSON");
        }
    } else {
        NSLog( @"nothing valid returned from the server...");
    }
} else {
    NSLog( @"no data back from the server");
}
于 2012-09-16T23:12:44.687 に答える
0

このようなことを考えていました

NSError *error = nil;
NSURLResponse *response = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost:3000/getusers"]];
[request setHTTPMethod:@"GET"];
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

JSONDecoder *decoder = [[JSONDecoder alloc]
                        initWithParseOptions:JKParseOptionNone];
NSArray *json = [decoder objectWithData:jsonData];

NSMutableArray *objects = [[NSMutableArray alloc] init];
NSMutableArray *keys = [[NSMutableArray alloc] init];
for (NSDictionary *user in json) {
    [objects addObject:[user objectForKey:@"user" ]];
    [keys addObject:[user objectForKey:@"value" ]];
}
users = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
NSLog(@"users: %@", users);
usersKeys = [users allKeys];

しかし、それは多くのアイテムにとって効率的ではないように見えますか、それとも間違っていますか?

于 2012-09-16T23:43:38.587 に答える