【问题标题】:How to json parsing in ios如何在ios中解析json
【发布时间】:2015-10-29 13:30:40
【问题描述】:

我是 ios 开发的新手,所以我需要从基础层面逐步指导,我使用了很多链接,但所有链接都为我提供了直接的项目代码,然后开始我需要了解所有的想法是如何像布局和开发一样工作的。

【问题讨论】:

标签: ios objective-c json


【解决方案1】:

你可以使用 NSJSONSerialization 类 https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/

NSDictionary *responseObject = [NSJSONSerialization
JSONObjectWithData:responseData options:self.readingOptions   
error:&serializationError];

【讨论】:

  • 然后详细询问您正在尝试做什么您尝​​试了什么以及什么不起作用。
  • 您的 json 对象已转换为 NSDictionary,因此您可以在任何情况下使用它,您还期待什么?说明你想怎么用?
  • 是的,我需要一步一步的例子。就像 step-1 创建 xcode 新项目。第 2 步等等....
【解决方案2】:

对于 json 解析,您可以使用 AFNetworking。 它非常简单和有用的平台,具有简单和复杂的 json 解析。

您可以使用以下链接了解更多信息

http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

【讨论】:

    【解决方案3】:

    对于json解析,您可以使用简单的NSJsonSerialization。您可以使用json解析post将数据发布到服务器,也可以使用json解析GET方法从服务器获取数据。

    将数据发布到服务器的第一个 POST

    //STEP 1 :Here give YOUR URL instead of my URL
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.3.128:8050/RestWebService/rest/person"]];
    
    
    //STEP 2 :create the Method "POST"
    [request setHTTPMethod:@"POST"];
    
    //STEP 3 :Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
    NSString *userUpdate =[NSString strin gWithFormat:@"user_email=%@&user_login=%@&user_pass=%@&  last_upd_by=%@&user_registered=%@&",txtemail.text,txtuser1.text,txtpass1.text,txtuser1.text,datestr,nil];
    
    //STEP 4 :Check The Value what we passed
    NSLog(@"the data Details is =%@", userUpdate);
    
    //STEP 5 :Convert the String to Data
    NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
    
    //STEP 7 :Apply the data to the body
    [request setHTTPBody:data1];
    
    //STEP 8 :Create the response and Error
    NSError *err;
    NSURLResponse *response;
    
    //STEP 9 :send synchronous request to server  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    
    //STEP 10 :getting the response 
    NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
    
    //STEP 11:Check whether we got the Response or Not
    NSLog(@"got response==%@", resSrt);
    if(resSrt)
    {
      NSLog(@"got response");
    }
    else
    {
       NSLog(@"faield to connect");
    }
    

    第二次使用 NSJsonSerialization 方法从服务器获取响应

    //STEP 1 :Here give YOUR URL instead of my URL
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"http://api.worldweatheronline.com/free/v1/search.ashx?query=London&num_of_results=3&format=json&key=xkq544hkar4m69qujdgujn7w"]];
    
    //STEP 2 :create the Method "GET" 
    [request setHTTPMethod:@"GET"];
    
    //STEP 3 : set request as Json format value type 
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
    
    //STEP 4 : Create the response and Error 
    NSError *err;
    NSURLResponse *response;
    
    //STEP 5 :send synchronous request to server  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&err];
    
    //You need to check response.Once you get the response copy that and paste in ONLINE JSON VIEWER.If you do this clearly you can get the correct results.    
    
    //After that it depends upon the json format whether it is DICTIONARY or ARRAY 
    
    //STEP 6 :Get the response to dictionary or array depends upon the response
    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-23
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多