我有JSON
格式要求。
{
"first_name" : "XYZ",
"last_name" : "ABC"
}
我的值为NSString
。
NSString strFName = @"XYZ";
NSString strLName = @"ABC";
NSString strKeyFN = @"first_name";
NSString strKeyLN = @"last_name";
我使用NSMutableDictionary
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
[dict setObject:strFName forKey:strKeyFN];
[dict setObject:strLName forKey:strKeyLN];
然后输出
{
first_name = XYZ,
last_name = ABC
}
所以我不希望“=”分开键和&值而不是我希望“:”分隔键和值
我已经完成了stack overflow questions的大部分工作,但仅在输出
中无法获得“=”那么请帮忙吗?
答案 0 :(得分:1)
这是你的答案:
NSString *strFName = @"XYZ";
NSString *strLName = @"ABC";
NSInteger number = 15;
NSString *strKeyFN = @"first_name";
NSString *strKeyLN = @"last_name";
NSString *numValue = @"Number";
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:strFName forKey:strKeyFN];
[dic setObject:strLName forKey:strKeyLN];
[dic setObject:[NSNumber numberWithInt:number] forKey:numValue];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSArray arrayWithObject:dic] options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON %@",jsonString);
答案 1 :(得分:0)
您在NSDictionary
之后编写此代码 .goto('https://wirecard-sandbox-engine.thesolution.com/engine/hpp/')
.use(iframe.withFrameName('wc', function (nightmare) {
nightmare
.type('#account_number', accountNumber)
.screenshot(resultfolder + '\\06-Card-Number.png')
.type('#card_security_code', testData.securityCode)
.selectIndex('#expiration_month_list', 1)
.selectIndex('#expiration_year_list', 4)
.click('span[id="hpp-form-submit"]')
}))
答案 2 :(得分:0)
试试这个: -
NSString *cleanedString1 =[strFName stringByReplacingOccurrencesOfString:@"/"" withString:@""];
NSString *cleanedString2 =[strLName stringByReplacingOccurrencesOfString:@"/"" withString:@""];
NSDictionary *Dict = [NSDictionary dictionaryWithObjectsAndKeys:
cleanedString1, strKeyFN,
cleanedString2, strKeyLN,nil];
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:Dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);
答案 3 :(得分:0)
NSString *strFName = @"XYZ";
NSString *strLName = @"ABC";
NSString *strKeyFN = @"first_name";
NSString *strKeyLN = @"last_name";
NSDictionary *ictionary = [NSDictionary dictionaryWithObjectsAndKeys:
strKeyFN, strFName,strKeyLN, strLName,nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"dictionary as string:%@", jsonString);
答案 4 :(得分:0)
您必须将NSMutableDictionary
转换为NSData
然后将NSData
转换为您想要的json
字符串
NSString *strFName = @"XYZ";
NSString *strLName = @"ABC";
NSString *strKeyFN = @"first_name";
NSString *strKeyLN = @"last_name";
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
[dict setObject:strFName forKey:strKeyFN];
[dict setObject:strLName forKey:strKeyLN];
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *jasonString= [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
答案 5 :(得分:0)
NSString *strFName = @"ABC";
NSString *strLName = @"XYZ";
NSString *strKeyFN = @"last_name";
NSString *strKeyLN = @"first_name";
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:strFName forKey:strKeyFN];
[dict setObject:strLName forKey:strKeyLN];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[NSArray arrayWithObject:dict] options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStrng = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Your required JSON is %@",jsonStrng);
答案 6 :(得分:0)
这个非常强大的代码实现了你的目标
NSDictionary *userDic = @{strKeyFN:strFName,strKeyLN:strLName};