首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为iphone使用json-framework

为iphone使用json-framework
EN

Stack Overflow用户
提问于 2012-06-27 18:53:12
回答 5查看 96关注 0票数 0

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

代码语言:javascript
复制
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);

我得到了,

代码语言:javascript
复制
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相同吗?

现在,我添加以下内容:

代码语言:javascript
复制
for (id key in [dictionary allKeys]) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

它崩溃了:

代码语言:javascript
复制
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数据?

提前谢谢你

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-06-27 19:22:52

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

代码语言:javascript
复制
NSArray *object = [serverOutput JSONValue];

现在解析它并按您的需要使用:

代码语言:javascript
复制
for (NSMutableDictionary *dict in object) {
    NSLog(@"key: %@", [dict allKeys]);
    NSLog(@"value_ID : %@", [dict objectForKey:@"id"]);
    NSLog(@"value_title : %@", [dict objectForKey:@"title"]);
}

希望这能帮到你……

票数 1
EN

Stack Overflow用户

发布于 2012-06-27 18:58:16

尝试修改此行。

代码语言:javascript
复制
NSDictionary* object = [serverOutput JSONValue];
//you dont need this
//NSMutableDictionary *dictionary = [object JSONRepresentation];

JSONRepresentation返回NSString,而NSString没有抛出异常的allKeys方法

JSONValue返回dictionary对象。

您的数据现在以object格式存储,您可以像这样获取它们

代码语言:javascript
复制
    for (NSString* key in [object allKeys]) {
        NSLog(@"key: %@, value: %@", key, [object objectForKey:key]);
    }

或按块

代码语言:javascript
复制
NSLog(@"id = %@", [object objectForKey:@"id"]);
NSLog(@"id = %@", [object objectForKey:@"title"]);
票数 2
EN

Stack Overflow用户

发布于 2012-06-27 19:03:58

代码语言:javascript
复制
 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是返回字符串

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11224584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档