2016-08-02 68 views
1

你好我有一个像这样的NSMutableArray。字典对象不能转换成JSON对象objective-c

<__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对象的错误是什么。 请帮帮我。 感谢

UPDATE

这是我想转换成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\"} 

WRONG STRING

{\"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\"} 
+0

你应该得到错误,如果JSON字典是零。什么是错误? – Sujit

+0

因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** + [NSJSONSerialization dataWithJSONObject:options:error:]:JSON中的顶级类型无效'这是我得到的错误 – Irrd

+0

我在使用作为漂亮打印的选项 – Irrd

回答

0

尝试

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

不要给任何选项。

+0

然后也无和错误味精是错误域= NSCocoaErrorDomain代码= 3840“字符842周围的无效值。” UserInfo = {NSDebugDescription =字符842周围的值无效} – Irrd

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"; 
}