修复Json使用值对创建

时间:2016-01-14 10:07:22

标签: objective-c json xcode

我已经在Json下面创建,但它不是我想要的输出请在关键消息和null中修复反转引号

    NSString *chID=@"101";
    NSString *reqId=@"REQID";
    NSString *enTypeId=@"100";
    NSString *mb=@"8802034079";


    NSString *MCode=@"21212";
    NSString *RKey=@"QWERTY";
    NSString *OTPCode=@"1414";
    NSString *ORKey=@"ORKEY1212";
    NSString *PinCode=@"null";

    NSDictionary *jsonoutPutDataDataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                     chID, @"chID",
                                                     reqId, @"reqId",
                                                     enTypeId, @"enTypeId",
                                                     mb, @"mb",
                                                     nil];


    NSDictionary *jsonDataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        jsonoutPutDataDataDictionary, @"outPutData",
                                        MCode, @"MCode",
                                        RKey, @"RKey",
                                        OTPCode, @"OTPCode",
                                        ORKey, @"ORKey",
                                        PinCode, @"PinCode",
                                        nil];



    NSDictionary *jsonmsgDataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                           jsonDataDictionary, @"message",
                                           nil];

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonmsgDataDictionary options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"jsonData as string:\n%@", jsonString);



 NSString *chID=@"101";
    NSString *reqId=@"REQID";
    NSString *enTypeId=@"100";
    NSString *mb=@"8802034079";


    NSString *MCode=@"21212";
    NSString *RKey=@"QWERTY";
    NSString *OTPCode=@"1414";
    NSString *ORKey=@"ORKEY1212";
    NSString *PinCode=@"null";

我的输出:

"message" ={
    "outPutData": {
        "chID": "101",
        "reqId": "REQID",
        "enTypeId": "100",
        "mb": "8802034079"
    },
    "MCode": "21212",
    "RKey": "QWERTY",
    "OTPCode": "1414",
    "ORKey": "ORKEY1212",
    "PinCode": "null"
}

需要输出:

message ={
    "outPutData": {
        "chID": "101",
        "reqId": "REQID",
        "enTypeId": "100",
        "mb": "8802034079"
    },
    "MCode": "21212",
    "RKey": "QWERTY",
    "OTPCode": "1414",
    "ORKey": "ORKEY1212",
    "PinCode": null
}

1 个答案:

答案 0 :(得分:2)

NSString *PinCode=@"null";

在这里添加字符串" null"而不是将空对象添加到字典中。相反,你应该使用:

NSNull *PinCode = [NSNull null];

键周围的引号是正确的,因为它们是字符串。