30

私のフィードURLはどこJSON_CATEGORY_DATA_URL_STRINGにありますか?

[
    {
        "group":"For Sale",
        "code":"SSSS"
    },
    {
        "group":"For Sale",
        "category":"Wanted",
        "code":"SWNT"
    }
]

私は次のコードから素晴らしいNSDictionary(または)を得ることができないようです:NSArray

+ (NSDictionary *)downloadJSON
{

NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];    
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
NSDictionary *json_dict = (NSDictionary *)json_string;
NSLog(@"json_dict\n%@",json_dict);
    NSLog(@"json_string\n%@",json_string);

return json_string;
}

私はこれに関する多くの投稿を読みましたが、それを取得していません。

4

5 に答える 5

101

IOS5では、JSON のシリアル化にNSJSONSerializationを使用できます。

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
于 2012-03-20T11:07:20.410 に答える
8

文字列を辞書としてキャストして、JSONを解析することを期待することはできません。その文字列を取得して辞書に変換するには、JSON解析ライブラリを使用する必要があります。

于 2011-02-18T06:39:44.407 に答える
2

この作業を簡単にするクラスを作成しました。iOS 5 の NSJSONSerialization を使用します。こちらの github からクローンします。

于 2013-01-24T14:53:46.233 に答える
1

JSONパーサーを使用する必要があります。これが編集されたコードです

+ (NSDictionary *)downloadJSON
{

NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];    
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
//JSONValue is a function that will return the appropriate object like dictionary or array depending on your json string.
NSDictionary *json_dict = [json_string JSONValue];
NSLog(@"json_dict\n%@",json_dict);
    NSLog(@"json_string\n%@",json_string);

return json_dict;
}

これは、NSDictionaryを取得するためのコードである必要があります。ただし、json文字列は配列であるため、代わりにを使用してください。

+ (NSArray *)downloadJSON
{

NSDictionary *json_string;
NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING];
NSLog(@"%@",dataURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]];    
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

json_string = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]autorelease];
NSArray *json_dict = [json_string JSONValue];
NSLog(@"json_dict\n%@",json_dict);
    NSLog(@"json_string\n%@",json_string);

return json_dict;
}

編集:JSONValueメソッドを呼び出すにはJSON.frameworkを使用する必要があります。また、タイプではなく、またはではなく、
戻る必要があります。 それはあなたのクラス変数なので、それを自動解放しないでくださいjson_dictjson_stringjson_stringNSStringNSDictionaryNSArray

于 2011-02-18T06:42:43.220 に答える