【问题标题】:Parse JSON - iPhone解析 JSON - iPhone
【发布时间】:2012-07-17 11:53:15
【问题描述】:

我是 json 新手,需要您的帮助。

我收到这样的 JSON 字符串:

{"network":
   {
   "network_id":111,
   "name":"test name",
   "city":"test city",
   "country":"test country",
   "description":"test desc"
   }
}

如何处理此字符串并拆分键/值以便在我的视图中使用它们?

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {    
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

    self.responseData = nil;

//*********** How I can parse responseString *********//

[networkIDLabel setText:@"ADD THE VALUE"];
[nameLabel setText:@"ADD THE VALUE"];


    [responseString release];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

谢谢

【问题讨论】:

标签: iphone ios json


【解决方案1】:

在 iOS 5 及更高版本中,您可以直接使用 NSJSONSerialization 解析响应数据:

[NSJSONSerialization JSONObjectWithData:self.responseData …];

如果你想支持早期版本的iOS,可以使用JSONKit

【讨论】:

  • 感谢 Marcelo,是的,我想支持早期版本
【解决方案2】:

在objective-c中json可以表示为字典

-(void)getData:(NSData*)response{

 // You have to include the SBJSON or else you can also use the NSJSONSerialization

 //NSDictionary *jsonData          =           [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&erro];

SBJSON *parse                               =           [[SBJSON alloc]init];

NSString *jsonString                        =           [[NSString alloc] initWithData:response
                                                                              encoding:NSUTF8StringEncoding]; 

NSDictionary *jsonData                      =           [parse objectWithString:jsonString error:&erro];
NSDictionary *insideData                          =           [jsonData objectForKey:@"network"];

if(![insideData isKindOfClass:[NSNull class]])
{

        NSString *data1         =       [insideData objectForKey:@"network_Id"];

         NSString *data2         =       [insideData objectForKey:@"name"];


 }

}

【讨论】:

  • 你有嵌套的 json 或者可以说嵌套字典所以我根据你的 json 编辑了我的答案
  • 谢谢你 naveen :) .. 我知道这个问题看起来很简单,但我不熟悉 NSDictionary 所以我无法解决它.. 再次感谢你 :))
  • 我可以在控制台上打印 data1,但我不能将它与标签一起使用! [label setText:[insideData objectForKey:@"network_Id"]];然后我崩溃了:(
  • -[NSDecimalNumber isEqualToString:]:无法识别的选择器发送到实例 0x6a5e530 2012-07-18 10:00:53.876 OAuthExample[839:f803] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: ' - [NSDecimalNumber isEqualToString:]:发送到实例0x6a5e530无法识别的选择' ***第一掷调用堆栈:(0x1400022 0x1591cd6 0x1401cbd 0x1366ed0 0x1366cb2 0x18a0ff 0x1b3d5 0xa4da49 0xa4be84 0xa4cea7 0xa4be3f 0xa4bfc5 0x990f5a 0x39d3a39 0x3aa0596 0x39ca120 0x3aa0117 0x39c9fbf 0x13d494f 0x1337b43 0x1337424 0x1336d84 0x1336c9b 0x12e97d8 0x12e988a 0x4a626 0x5932 0x28a5)
  • [networkIDLabel setText:[[dic objectForKey:@"network"] objectForKey:@"network_id"]];不要这样做试试这个 NSString *networkId = [dic objectForKey:@"network_ID"]; [networkIDLabel setText:[NSString stringWithFormat:@"%@",networkId]];
猜你喜欢
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
相关资源
最近更新 更多