首先用你的“元”和“数据”信息填充两个 NSDictionaries。然后将它们添加到主 NSDictionary 中,然后使用 NSJsonSerialization 进行序列化
NSDictionary * metaDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"appId", @"S3B9CU4R2B9JTXV9254Y", @"appVersion", @"2.1.0", @"serverVersion", @"1.1.0", @"platform", @"iOS",..., nil];
NSDictionary * dataDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"userName", @"rrullo", @"password", @"rrullo!", nil];
NSDictionary * mainDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"data", dataDict, @"meta", metaDict, nil];
现在您可以使用 NSJsonSerialization 将该 mainDict 转换为 NSString
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mainDict
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
希望这可以帮助