如何在 iOS 中将字符串转换为 NSArray
How to convert string to NSArray in iOS
我有一个 JSON
对象,它有一个像这样的键值对
"AttachedDocument": "[{\"DocumentId\":354,\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",\"DocumentType\":\"image/png\"}]"
这个值是一个字符串。我怎样才能把它转换成 NSArray
完整对象
{
"LeaveEntryCode": 0,
"RequestId": 0,
"EmployeeCode": 17186,
"LeaveYear": 2016,
"LeaveTypeCode": 1,
"LeaveReasonCode": 0,
"BaseType": "ess",
"StartDate": "2016-10-24T00:00:00",
"EndDate": "2016-10-24T00:00:00",
"NoOfDays": 1,
"StartDateSession": "full",
"EndDateSession": "full",
"PreApproved": false,
"ForDate": "1901-01-01T00:00:00",
"Remarks": "test from Android",
"CoveringPersonCode": 0,
"AttachedDocument": "[{\"DocumentId\":354,\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",\"DocumentType\":\"image/png\"}]",
"RequestStatus": "P",
"Deleted": false,
"Status": false,
"CreatedBy": 0,
"CreatedDate": "0001-01-01T00:00:00",
"UpdatedBy": 0,
"UpdatedDate": "0001-01-01T00:00:00",
"DeletedBy": 0,
"DeletedDate": "0001-01-01T00:00:00",
"ModuleId": 2,
"ObjectId": 20,
"StartDateString": "10/24/2016",
"EndDateString": "10/24/2016",
"LeaveDayList": [
"10/24/2016-FH,10/24/2016-SH"
],
"SystemLeaveTypeCode": "ANN",
"LeaveTypeName": "ANNUAL",
"Employee": null,
"LieuDayList": null,
"BaseLeaveType": "ANN",
"CoveringPersonName": null,
"LeaveReasonName": "test",
"DocumentSource": "LEAVE"
}
这是我得到的完整 JSON
对象。我是通过网络服务获取的。
实际上您得到的是 josn string
作为响应,因此您可以将其转换为 josn object
类似
NSString *yourString = @"[{\"DocumentId\":354,\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",\"DocumentType\":\"image/png\"}]";
NSError *error;
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSLog(@"json object : %@",jsonObject);
此处yourString
表示AttachedDocument
键的对象!
我有一个 JSON
对象,它有一个像这样的键值对
"AttachedDocument": "[{\"DocumentId\":354,\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",\"DocumentType\":\"image/png\"}]"
这个值是一个字符串。我怎样才能把它转换成 NSArray
完整对象
{
"LeaveEntryCode": 0,
"RequestId": 0,
"EmployeeCode": 17186,
"LeaveYear": 2016,
"LeaveTypeCode": 1,
"LeaveReasonCode": 0,
"BaseType": "ess",
"StartDate": "2016-10-24T00:00:00",
"EndDate": "2016-10-24T00:00:00",
"NoOfDays": 1,
"StartDateSession": "full",
"EndDateSession": "full",
"PreApproved": false,
"ForDate": "1901-01-01T00:00:00",
"Remarks": "test from Android",
"CoveringPersonCode": 0,
"AttachedDocument": "[{\"DocumentId\":354,\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",\"DocumentType\":\"image/png\"}]",
"RequestStatus": "P",
"Deleted": false,
"Status": false,
"CreatedBy": 0,
"CreatedDate": "0001-01-01T00:00:00",
"UpdatedBy": 0,
"UpdatedDate": "0001-01-01T00:00:00",
"DeletedBy": 0,
"DeletedDate": "0001-01-01T00:00:00",
"ModuleId": 2,
"ObjectId": 20,
"StartDateString": "10/24/2016",
"EndDateString": "10/24/2016",
"LeaveDayList": [
"10/24/2016-FH,10/24/2016-SH"
],
"SystemLeaveTypeCode": "ANN",
"LeaveTypeName": "ANNUAL",
"Employee": null,
"LieuDayList": null,
"BaseLeaveType": "ANN",
"CoveringPersonName": null,
"LeaveReasonName": "test",
"DocumentSource": "LEAVE"
}
这是我得到的完整 JSON
对象。我是通过网络服务获取的。
实际上您得到的是 josn string
作为响应,因此您可以将其转换为 josn object
类似
NSString *yourString = @"[{\"DocumentId\":354,\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",\"DocumentType\":\"image/png\"}]";
NSError *error;
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSLog(@"json object : %@",jsonObject);
此处yourString
表示AttachedDocument
键的对象!