2016-10-25 1 views
0

Bonjour J'ai 2 chaînes JSON comme ceci.Chaîne JSON donnant NULL après la désérialisation iOS

1er objet

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

2ème objet

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

Je veux obtenir la valeur AttachedDocument dans un tableau. Donc j'ai fait comme ça.

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


} 

Mon 1er objet me donnant la valeur correcte pour str variable. Mais 2ème objet me donner pour AttachedDocument valeur

Je mettais seulement la valeur de clé de cette AttachedDocument

1er objet de

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

2ème objet de

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

Pouvez-vous s'il vous plaît expliquez-moi quelle est la différence (plutôt une image/png et JPG). Pourquoi mon deuxième objet me donne null pour AttachedDocument S'il vous plaît aidez-moi. Merci

+0

Formatez vos échantillons JSON en différentes lignes afin qu'il soit plus facile de voir leur structure. –

+0

FYI - n'utilisez pas 'valueForKey:' pour obtenir un objet d'un 'NSDictionary'. Utilisez 'objectForKey:' sauf si vous avez un besoin spécifique et clairement compris d'utiliser le codage de valeurs-clés. – rmaddy

+0

@rmaddyJ'ai utilisé objectfor key pour obtenir ma chaîne, mais je reçois cette erreur *** Termination app en raison de l'exception uncaught 'NSInvalidArgumentException', raison: '- [NSNull dataUsingEncoding:]: sélecteur non reconnu envoyé à l'instance – Irrd

Répondre

0

Oui 1 er objet est un format JSON valide.

Mais quand nous vérifions les 2 objets

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

} 

Deux objets JSON ont le même qui est la raison pour laquelle 2ème objet valeur NULL lorsque donne désérialiser il .