【问题标题】:How to parse received response [closed]如何解析收到的响应[关闭]
【发布时间】:2013-11-08 12:22:54
【问题描述】:

我是 iOS 应用程序开发的新手,我从服务器收到以下响应:

"[{\"EmployeeID\":\"000001\",\"EmplyeeName\":\"ABCD EFGHI\"},
{\"EmployeeID\":\"000002\",\"EmplyeeName\":\"ADGHT ASASASAS\"}]"

请任何人帮助我了解如何在我的应用程序中使用员工 ID 和员工姓名。

【问题讨论】:

  • 请张贴您的网址...
  • 你尝试了什么?在发布问题之前,您是否进行了任何研究?
  • @Martin R:我做了太多的研究,我没有得到正确的解决方案,这就是我发布这个问题的原因。
  • 但是你应该展示你已经尝试过的东西。显示您的代码、您得到的结果以及您的问题所在!
  • NSURLurl = [NSURL URLWithString:@"XXXXXXXX.sr/LoginAuth.svc/Employee"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; if (request) { [request setURL: url]; 连接 = [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection)connection didReceiveResponse:(NSURLResponse*)response; { webData = [[NSMutableData alloc] init ]; }

标签: ios json


【解决方案1】:

您的 JSON 数据看起来像“嵌套 JSON”,这意味着您必须对其进行两次反序列化。

第一次反序列化从您的 JSON 数据中提取一个字符串:

NSData *response = ...; // your data
NSError *error;
NSString *innerJson = [NSJSONSerialization JSONObjectWithData:response
                              options:NSJSONReadingAllowFragments error:&error];

现在innerJson 是字符串

[{"EmployeeID":"000001","EmplyeeName":"ABCD EFGHI"},{"EmployeeID":"000002","EmplyeeName":"ADGHT ASASASAS"}]

这又是 JSON 数据。第二次反序列化提取数组:

NSArray *entries = [NSJSONSerialization JSONObjectWithData:[innerJson dataUsingEncoding:NSUTF8StringEncoding]
                              options:0 error:&error];

现在你可以像这样访问它了

for (NSDictionary *entry in entries) {
    NSString* employeeID = [entry objectForKey:@"EmployeeID"];
    NSLog(@"%@", employeeID);
}

【讨论】:

  • 非常感谢 Martin R 和 Alex,它正在无缝运行。
  • 从技术上讲,它只是“转义”,而不是“嵌套”。但是上面对 NSJSONSerialization 的使用是一种(几种)“取消转义”它的方法。
  • @HotLicks: 如果[{"foo":"bar"}] 从服务器以"[{\"foo\":\"bar\"}]" 发送,那么 1) 所有引号都被转义并且 2) 结果用引号括起来。这就是为什么我认为它是一个 JSON 字符串对象,它的值(在这种情况下)是一个 JSON 数组对象,并选择了术语“嵌套 JSON”。 - 当然还有其他方法,但 NSJSONSerialization 对我来说似乎是最简单且最不容易出错的,例如,如果 ["foo\"bar"] 从服务器发送为 "[\"foo\\\"bar\"]"
  • JSON 字符串仅有的两个合法的外部结构是“object”(字典)和“array”。 “String”不是独立的法律地位,这就是为什么您必须指定“AllowFragments”才能让解析器接受它。 (但是使用 NSJSONSerialization 来“取消转义”字符串是可以的,尽管毫无疑问有更快的方法,如果你真的很注重效率的话。)
  • @HotLicks:我想我现在明白你的意思了。
【解决方案2】:
NSData *response = ...;
NSArray *entries = [NSJSONSerialization JSONObjectWithData:response
                                                   options:0
                                                     error:nil];

for(NSDictionary* entry in entries) {
    NSString* employeeID = [entry objectForKey:@"EmployeeID"];
}

【讨论】:

  • 谢谢 ALex,上面的代码没有运气,你能告诉我下面的代码哪里错了吗? - (void)connectionDidFinishLoading:(NSURLConnection*)connection {NSString* s = [[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding];NSLog(@"response %@", s); NSData* 响应 = [s dataUsingEncoding:NSUTF8StringEncoding]; NSArray entries = [NSJSONSerialization JSONObjectWithData:response options:0 error:nil]; for(NSDictionary entry in entries) { NSString* employeeID = [entry objectForKey:@"EmployeeID"]; NSLog(@"打印 %@", 员工 ID); } }
  • 为什么不使用“NSData* response = webData;”?
  • 用过的螺母没有运气显示没有结果
  • 您尝试过 Martin R“嵌套 JSON”解决方案吗?看起来应该可以工作
  • 非常感谢 Martin R 和 Alex,它正在无缝运行。
【解决方案3】:

查看 JSON Parser,您的响应是 JSON,因此您需要从 json 获取该数据。

NSURL * url=[NSURL URLWithString:@"<YourURL>"];

NSData * data=[NSData dataWithContentsOfURL:url];

NSError * error;

NSMutableDictionary  * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

NSLog(@"%@",json);


NSMutableArray * EmployeeID=[[NSMutableArray alloc]init];

NSMutableArray * EmployeeName=[[NSMutableArray alloc]init];

NSArray * responseArr = json[@"geonames"];

for(NSDictionary * dict in responseArr)
{

[EmployeeID addObject:[dict valueForKey:@"EmployeeID"]];
[EmployeeName addObject:[dict valueForKey:@"EmplyeeName"]];

}

现在你得到了 EmployeeID,EmployeeName 数组,你可以在任何你想要的地方使用。

【讨论】:

    【解决方案4】:

    【讨论】:

      【解决方案5】:

      使用这个:

      NSData* yourData = [[NSData alloc] initWithContentsOfURL: yourURL];
      NSarray* yourJSON = [NSJSONSerialization JSONObjectWithData:yourData options:kNilOptions error:nil];
      

      希望对你有帮助:)

      【讨论】:

        【解决方案6】:

        尝试使用这个:

        NSDictionary *myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
        

        【讨论】:

          猜你喜欢
          • 2012-08-02
          • 2011-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多