【问题标题】:Parsing JSON in iOS application在 iOS 应用程序中解析 JSON
【发布时间】:2014-07-14 11:47:01
【问题描述】:

由于我是iOS开发新手,请帮我解析JSON输出。

我的 JSON 输出:

{"sbi":[{"Emp_Id":1001,"Emp_Name":"James","Designation":"Manager","Skills":["C,C++"]},{"Emp_Id":1002,"Emp_Name":"John","Designation":"Asst.Manager","Skills":["Java,PHP"]},{"Emp_Id":1003,"Emp_Name":"Joe","Designation":"Chief Manager","Skills":["Oracle,HTML"]}]}

当我们启动一个应用程序时,我应该在第一个视图中获得sbi,如果我选择该特定行,我应该在下一个视图中获得与sbi 相关的所有详细信息,即EmpId、@987654326 @, Designation, Skills, ...

提前致谢。

【问题讨论】:

  • 查看 API 文档和其他数百个问题。基本上:+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error
  • 添加您的代码以获得响应。
  • 这和gson有什么关系?

标签: ios xcode gson


【解决方案1】:
    -(void)Startconnection:(NSString *)urlString
{
   NSLog(@"%@",urlString); 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:"your url string "]];
 connetion1=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    webData = [NSMutableData data];

}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
 //   [ShowAlert showMyAlert:@"Network Alert" :@"No Internet connection detected"];

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSError *myError = nil;
    responceDic=nil;
 responceDic = [NSJSONSerialization JSONObjectWithData:webData options:NSJSONReadingMutableLeaves error:&myError];

    NSLog(@"%@",responceDic  );
}

在.h文件中声明NSXMLParserDelegate委托

@interface webservice : NSObject<NSXMLParserDelegate>
{

    NSMutableData *  webData;
    NSString *currentData;
    NSURLConnection * connetion1;

}

【讨论】:

  • 获取响应 NSLog(@"%@",[[responceDic objectforkey:@"sbi"] objectforkey:@"Emp_Id"]);
【解决方案2】:

将 json 解析为字典对象

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:yourJson options:0 error:nil];

【讨论】:

    【解决方案3】:

    请参考以下链接到 Json 解析演示。它将帮助您学习 Json 解析。

    http://www.raywenderlich.com/5492/working-with-json-in-ios-5

    【讨论】:

    • 这个问题有点矫枉过正。
    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2013-03-20
    相关资源
    最近更新 更多