0

我正在尝试读取我的 json 数据,但它崩溃了。

NSString *hostStr = @"http://www.myIP.com/notices.php";

NSLog(@"%@",hostStr);

NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
//NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];


NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
id object = [serverOutput JSONValue];
NSMutableDictionary *dictionary = [object JSONRepresentation];


NSLog(@"serverOutput %@", serverOutput);
NSLog(@"Dictionary %@", dictionary);

我有,

2012-06-27 12:47:51.138 usualBike[1520:f803] serverOutput [{"id":"128","title":"Usual Bike llega a Tres Cantos"},{"id":"127","title":"Sol y Bicis "},{"id":"126","title":"Usual Bike reduce los tiempos de espera"}]
2012-06-27 12:47:51.139 usualBike[1520:f803] Dictionary [{"id":"128","title":"Usual Bike llega a Tres Cantos"},{"id":"127","title":"Sol y Bicis "},{"id":"126","title":"Usual Bike reduce los tiempos de espera"}]

字典应该与 serverOutput 相同吗?

现在我添加这个:

for (id key in [dictionary allKeys]) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

它崩溃了:

2012-06-27 12:51:40.652 usualBike[1550:f803] -[__NSCFString allKeys]: unrecognized selector sent to instance 0x88683f0
2012-06-27 12:51:40.653 usualBike[1550:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString allKeys]: unrecognized selector sent to instance 0x88683f0'
*** First throw call stack:
(0x13e7022 0x1578cd6 0x13e8cbd 0x134ded0 0x134dcb2 0x2b2a 0xf8a1e 0x1140a9 0x113edd 0x1124aa 0x11234f 0x113e14 0x13e8e99 0x3414e 0x340e6 0x25c4bd 0x13e8e99 0x3414e 0x340e6 0xdaade 0xdafa7 0xdab13 0x25ec48 0x13e8e99 0x3414e 0x340e6 0xdaade 0xdafa7 0xda266 0x593c0 0x595e6 0x3fdc4 0x33634 0x12d1ef5 0x13bb195 0x131fff2 0x131e8da 0x131dd84 0x131dc9b 0x12d07d8 0x12d088a 0x31626 0x1f8d 0x1ef5)
terminate called throwing an exception(lldb)

如何读取 json 数据?

先感谢您

4

5 回答 5

2

尝试修改这一行。

NSDictionary* object = [serverOutput JSONValue];
//you dont need this
//NSMutableDictionary *dictionary = [object JSONRepresentation];

JSONRepresentation返回NSString,并且NSString没有allKeys引发异常的方法

whileJSONValue返回dictionary对象。

你的数据现在在object你可以得到他们喜欢

    for (NSString* key in [object allKeys]) {
        NSLog(@"key: %@, value: %@", key, [object objectForKey:key]);
    }

或分段

NSLog(@"id = %@", [object objectForKey:@"id"]);
NSLog(@"id = %@", [object objectForKey:@"title"]);
于 2012-06-27T10:58:16.083 回答
1
 NSMutableDictionary *complaints =[NSJSONSerialization JSONObjectWithData:respo options:kNilOptions error:&error];
 NSMutableArray *JSONAry = [complaints  objectForKey:@"posts"];
 NSMutableArray *tempary =[[NSMutableArray alloc]init];
 for (int i=0;i < [JSONAry count];i++) {
       ResultFatch *rs = [[ResultFatch alloc] initWithId:[[JSONAry objectAtIndex:i]objectForKey:@"id"]
                                                     title :[[JSONAry objectAtIndex:i] objectForKey:@"title"]];

      [tempary addObject:rs];
  }
 NSLog(@"%@",tempary);

ResultFatch 类是返回字符串

于 2012-06-27T11:03:58.333 回答
1

你有一个字典数组..使用以下代码: -

NSArray *object = [serverOutput JSONValue];

现在解析它并根据需要使用:-

for (NSMutableDictionary *dict in object) {
    NSLog(@"key: %@", [dict allKeys]);
    NSLog(@"value_ID : %@", [dict objectForKey:@"id"]);
    NSLog(@"value_title : %@", [dict objectForKey:@"title"]);
}

这可能会帮助你....

于 2012-06-27T11:22:51.637 回答
0

使用此代码

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"response String= %@",responseString);
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *data = (NSDictionary *) [parser objectWithString:responseString error:nil];   
    NSString *com=(NSString *) [data objectForKey:@"id"];    
    NSLog(@"YOur id value= %@",com);
}
于 2012-06-27T11:00:34.583 回答
-1

使用 json 库。

SBJsonParser *parser;   
NSDictionary *resDict = [parser objectWithString: serverOutput error:nil];
于 2012-06-27T11:03:08.763 回答