我把json串上传到服务端就好
NSDictionary *values = [[NSDictionary alloc]initWithObjectsAndKeys:@"name", @"objective", nil];
NSArray *array = [[NSArray alloc]initWithObjects:values,nil];
NSDictionary *result = [[NSDictionary alloc]initWithObjectsAndKeys:array, @"objectives", nil];
我使用以下代码将字典转换为 JSON:
id obj = result;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:kNilOptions error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
我得到了正确的 json,例如
{ "objectives":[
{"objective":"name"}
]
}
这是正确的 json 但我需要类似 json 的字符串
{ objectives:[
{"objective":"name"}]
}
是否可以在 iOS 中创建不带双引号的键?
只有这个 json 字符串我们才能得到响应,否则我会得到错误。
最佳答案
如果它只是您要从中删除引号的第一个键,您可以使用以下快速解决方法
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\"objectives\"" withString:@"objectives"]];
关于ios - 我需要在 ios 的键中不带双引号的 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817932/