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

时间:2022-11-29 16:57:21

hello I have a NSMutableArray like this.

你好我有这样的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;
};
}
)

I want to convert this to JSON object. So I did like this.

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

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

but my jsondictionary is always nil. What is the error with this RequestForm object. Please help me. Thanks

但我的jsondictionary总是零。这个RequestForm对象的错误是什么。请帮帮我。谢谢

UPDATE

This is the string I want to convert into a json.

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

UPDATE - CORRECT STRING

更新 - 正确的字符串

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

2 个解决方案

#1


1  

Its working Fine

它的工作很好

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

#2


0  

Try

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

Don't give any options.

不要给任何选择。

#1


1  

Its working Fine

它的工作很好

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

#2


0  

Try

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

Don't give any options.

不要给任何选择。