反序列化iOS后,JSON字符串给出NULL

时间:2016-10-25 03:12:23

标签: ios nsjsonserialization

您好我有2 JSON个字符串。

第一个对象

 {
   "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.0,
   "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"
}

第二个对象

{
   "LeaveEntryCode":0,
   "RequestId":0,
   "EmployeeCode":17227,
   "LeaveYear":2016,
   "LeaveTypeCode":1,
   "LeaveReasonCode":3,
   "BaseType":"ess",
   "StartDate":"2016-10-26T00:00:00",
   "EndDate":"2016-10-27T00:00:00",
   "NoOfDays":2.0,
   "StartDateSession":"full",
   "EndDateSession":"half",
   "PreApproved":false,
   "ForDate":"1901-01-01T00:00:00",
   "Remarks":"Test attachments in things to do",
   "CoveringPersonCode":0,
   "AttachedDocument":null,
   "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/26/2016",
   "EndDateString":"10/27/2016",
   "LeaveDayList":[
      "10/26/2016-FH,10/26/2016-SH,10/27/2016-FH,10/27/2016-SH"
   ],
   "SystemLeaveTypeCode":"ANN",
   "LeaveTypeName":"ANNUAL",
   "Employee":null,
   "LieuDayList":null,
   "BaseLeaveType":"ANN",
   "CoveringPersonName":"",
   "LeaveReasonName":"test",
   "DocumentSource":"LEAVE",
   "AttachedDocument":"[{\"DocumentId\":352,\"DocumentName\":\"IMG_2322.JPG\",\"DocumentType\":\"JPG\"}]"
}

我希望将AttachedDocument值放入数组中。所以我确实喜欢这个。

NSError *jsonError;
NSData *objectData = [[[[dm.mutArraySelectedReq objectAtIndex:index] objectForKey:@"RequestDetails"] valueForKey:@"RequestForm"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                     options:0
                                                       error:&jsonError];

if (json!=NULL) {//AttachedDocument
    NSString *str=[json valueForKey:@"AttachedDocument"];
    NSData *data2=[str dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json2 = [NSJSONSerialization JSONObjectWithData:data2
                                                         options:0
                                                           error:&jsonError];
    if (json2!=NULL) {
        NSArray *arrayAttached=[NSArray arrayWithObject:json2];
        dm.mutArrayAttachedDocs=[[NSMutableArray alloc] initWithArray:arrayAttached];
    }


}

我的第一个对象为str变量提供了正确的值。但第二个对象给了我AttachedDocument

我只放置了AttachedDocument的键值

第一个对象

"AttachedDocument": "[
{
\"DocumentId\":354,
\"DocumentName\":\"Screenshot_2016-04-15-00-35-11.png\",
\"DocumentType\":\"image/png\"
}
]"

第二个对象

"AttachedDocument":"[
{
\"DocumentId\":352,
\"DocumentName\":\"IMG_2322.JPG\",
\"DocumentType\":\"JPG\"
}
]"

你能解释一下差异是什么(而不是image / png和JPG)。为什么我的第二个对象为AttachedDocument提供了null 请帮我。 感谢

1 个答案:

答案 0 :(得分:0)

是1对象是有效的JSON格式。

但是当我们检查2个对象时

{
    "LeaveEntryCode": 0,
    "RequestId": 0,
    "EmployeeCode": 17227,
    "LeaveYear": 2016,
    "LeaveTypeCode": 1,
    "LeaveReasonCode": 3,
    "BaseType": "ess",
    "StartDate": "2016-10-26T00:00:00",
    "EndDate": "2016-10-27T00:00:00",
    "NoOfDays": 2.0,
    "StartDateSession": "full",
    "EndDateSession": "half",
    "PreApproved": false,
    "ForDate": "1901-01-01T00:00:00",
    "Remarks": "Test attachments in things to do",
    "CoveringPersonCode": 0,
  

" AttachedDocument":null,

    "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/26/2016",
    "EndDateString": "10/27/2016",
    "LeaveDayList": [
        "10/26/2016-FH,10/26/2016-SH,10/27/2016-FH,10/27/2016-SH"
    ],
    "SystemLeaveTypeCode": "ANN",
    "LeaveTypeName": "ANNUAL",
    "Employee": null,
    "LieuDayList": null,
    "BaseLeaveType": "ANN",
    "CoveringPersonName": "",
    "LeaveReasonName": "test",
    "DocumentSource": "LEAVE",
  

" AttachedDocument&#34 ;:   " [{\" DocumentId \":352,\" DocumentName \":\" IMG_2322.JPG \" \&#34 ; DocumentType \":\" JPG \"}]"

}

两个JSON对象具有相同的原因,即第二个对象在反序列化时给出NULL值的原因

相关问题