如何解析收到的回复

如何解析收到的回复

本文介绍了如何解析收到的回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iOS应用程序开发非常陌生,并且从服务器收到以下响应:

I am very new to iOS application development and I am getting the below response from the server:

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

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

Please anybody help me out on how to use employee ID and employee name in my application.

推荐答案

您的JSON数据看起来像嵌套JSON",这意味着您必须将其反序列化两次.

Your JSON data looks like "nested JSON", which means that you have to deserialize it twice.

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

The first deserialization extracts a string from your JSON data:

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数据.第二个反序列化提取数组:

which is again JSON data. The second deserialization extracts the array:

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

现在您可以像访问它一样

And now you can access it like

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

这篇关于如何解析收到的回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:02