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

whileはオブジェクトをJSONValue返し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 に答える