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,您可以使用NSJSONSerialization序列化 JSON。

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;
}

编辑:您需要使用 JSON.framework 来调用 JSONValue 方法。
您还需要返回json_dict而不是按json_string原样返回json_stringNSString不是返回NSDictionaryNSArray
并且不要自动释放它,因为它是你的类变量

于 2011-02-18T06:42:43.220 回答
0

创建获取json数据的方法。在urlwithstring中传递您的url。

-(void)fetchjsondata
{
     NSString *login= [[NSString stringWithFormat:@"your url string"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"----%@", login);
    NSURL *url = [NSURL URLWithString:[login stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    //-- Get request and response though URL
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];


    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               dispatch_async(dispatch_get_main_queue(), ^{
                                   if (data) {
                                       dic_property= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                                       NSLog(@"%@", dic_property);
                                       NSLog(@"counts=%d",[[dic_property objectForKey:@"Data"]count]);



                                   }
                                   else {
                                       NSLog(@"network error, %@", [error localizedFailureReason]);
                                   }
                               });

                           }];

}

在任何地方调用 fetchjsonmethod。

[NSThread detachNewThreadSelector:@selector(fetchdata) toTarget:self withObject:nil];

于 2016-02-02T11:39:09.277 回答