Dictionary对象无法转换为JSON对象objective-c

时间:2016-08-02 05:30:29

标签: ios json

你好我有这样的NSMutableArray。

<__NSArrayM 0x137e9f270>(
 {
  CanLoadMore = 0;
  IsFinalLevel = 1;
  NextLevelApprovers =     (
);
OrgStructure =     (
            {
        CreatedBy = 1;
        CreatedDate = "2015-07-29T12:10:34.297";
        Deleted = 0;
        DeletedBy = 0;
        DeletedDate = "1901-01-01T00:00:00";
        EntityHeadCode = 17098;
        EntityHeadName = "<null>";
        IsPermitted = 0;
        LegislativeCode = LKA;
        LevelId = 1;
        NodeId = 1;
        OrgEntity = Group;
        OrgLevelDescription = "";
        OrgLevelName = "ABCD Holdings";
        OrgStructureId = 1;
        ParentNodeId = 0;
        RefOrgLevelId = 1;
        Status = 1;
        UpdatedBy = 17113;
        UpdatedDate = "2016-04-07T08:53:13.727";
    },
            {
        CreatedBy = 17113;
        CreatedDate = "2016-04-06T12:17:19.75";
        Deleted = 0;
        DeletedBy = 0;
        DeletedDate = "1901-01-01T00:00:00";
        EntityHeadCode = 0;
        EntityHeadName = "<null>";
        IsPermitted = 0;
        LegislativeCode = LKA;
        LevelId = 2;
        NodeId = 2;
        OrgEntity = Company;
        OrgLevelDescription = "";
        OrgLevelName = "ABCD HCM";
        OrgStructureId = 2;
        ParentNodeId = 1;
        RefOrgLevelId = 6;
        Status = 1;
        UpdatedBy = 17098;
        UpdatedDate = "2016-07-29T11:14:12.513";
    }
);
PreviousLevelCount = 0;
RequestDetails =     {
    AutoId = 20;
    LevelId = 1;
    ModuleId = 2;
    NoOfLevels = 1;
    ObjectId = 20;
    ObjectName = "Leave Request";
    RequestForm = "{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17227,\"LeaveYear\":2016,\"LeaveTypeCode\":1,\"LeaveReasonCode\":2,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-02T00:00:00\",\"EndDate\":\"2016-08-02T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"half\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"Test 1\",\"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\":\"08/02/2016\",\"EndDateString\":\"08/02/2016\",\"LeaveDayList\":[\"08/02/2016-FH,08/02/2016-SH\"],\"SystemLeaveTypeCode\":\"ANN\",\"LeaveTypeName\":\"ANNUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}";
    RequestId = 20;
    RequestedDate = "2016-08-02T05:07:07.127";
    WorkflowId = 2;
};
RequesterDetails =     {
    AdminRequesterName = "";
    DisplayName = "<null>";
    EmployeeCode = 17227;
    EmployeeNumber = MM0000019;
    EtfNo = "";
    Gender = Female;
    ImagePath = "/profile/image/759006c5e4214f0";
    Name = haniAAAA;
};
}
)

我想将其转换为JSON对象。所以我确实喜欢这个。

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

但我的json字典始终为nil。这个RequestForm对象有什么错误。 请帮我。 谢谢

更新

这是我要转换为json的字符串。

{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17227,\"LeaveYear\":2016,\"LeaveTypeCode\":1,\"LeaveReasonCode\":2,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-02T00:00:00\",\"EndDate\":\"2016-08-02T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"half\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"Test 1\",\"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\":\"08/02/2016\",\"EndDateString\":\"08/02/2016\",\"LeaveDayList\":[\"08/02/2016-FH,08/02/2016-SH\"],\"SystemLeaveTypeCode\":\"ANN\",\"LeaveTypeName\":\"ANNUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}

更新 - 正确的字符串

{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17167,\"LeaveYear\":2016,\"LeaveTypeCode\":2,\"LeaveReasonCode\":0,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-01T00:00:00\",\"EndDate\":\"2016-08-01T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"full\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"\",\"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\":\"08/01/2016\",\"EndDateString\":\"08/01/2016\",\"LeaveDayList\":[\"08/01/2016-FH,08/01/2016-SH\"],\"SystemLeaveTypeCode\":\"CAS\",\"LeaveTypeName\":\"CASUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":null,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}

错误的字符串

{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17227,\"LeaveYear\":2016,\"LeaveTypeCode\":1,\"LeaveReasonCode\":2,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-02T00:00:00\",\"EndDate\":\"2016-08-02T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"half\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"Test 1\",\"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\":\"08/02/2016\",\"EndDateString\":\"08/02/2016\",\"LeaveDayList\":[\"08/02/2016-FH,08/02/2016-SH\"],\"SystemLeaveTypeCode\":\"ANN\",\"LeaveTypeName\":\"ANNUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}

2 个答案:

答案 0 :(得分:1)

工作正常

NSString *str = @"{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17167,\"LeaveYear\":2016,\"LeaveTypeCode\":2,\"LeaveReasonCode\":0,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-01T00:00:00\",\"EndDate\":\"2016-08-01T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"full\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"\",\"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\":\"08/01/2016\",\"EndDateString\":\"08/01/2016\",\"LeaveDayList\":[\"08/01/2016-FH,08/01/2016-SH\"],\"SystemLeaveTypeCode\":\"CAS\",\"LeaveTypeName\":\"CASUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":null,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}" ;

    NSError *jsonError;
    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
                                                         options:NSJSONReadingMutableContainers
                                                           error:&jsonError];



**OutPut**

Printing description of json:
{
    AttachedDocument = "<null>";
    BaseLeaveType = ANN;
    BaseType = ess;
    CoveringPersonCode = 0;
    CoveringPersonName = "<null>";
    CreatedBy = 0;
    CreatedDate = "0001-01-01T00:00:00";
    Deleted = 0;
    DeletedBy = 0;
    DeletedDate = "0001-01-01T00:00:00";
    DocumentSource = LEAVE;
    Employee = "<null>";
    EmployeeCode = 17167;
    EndDate = "2016-08-01T00:00:00";
    EndDateSession = full;
    EndDateString = "08/01/2016";
    ForDate = "1901-01-01T00:00:00";
    LeaveDayList =     (
        "08/01/2016-FH,08/01/2016-SH"
    );
    LeaveEntryCode = 0;
    LeaveReasonCode = 0;
    LeaveReasonName = "Leave TypeCasual - Leave - Leave Reason";
    LeaveTypeCode = 2;
    LeaveTypeName = CASUAL;
    LeaveYear = 2016;
    LieuDayList = "<null>";
    ModuleId = 2;
    NoOfDays = 1;
    ObjectId = 20;
    PreApproved = 0;
    Remarks = "";
    RequestId = 0;
    RequestStatus = P;
    StartDate = "2016-08-01T00:00:00";
    StartDateSession = full;
    StartDateString = "08/01/2016";
    Status = 0;
    SystemLeaveTypeCode = CAS;
    UpdatedBy = 0;
    UpdatedDate = "0001-01-01T00:00:00";
}

答案 1 :(得分:0)

尝试

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                                 options:0
                                                                  error:&jsonError];

不要给出任何选择。