更新时间:2022-12-10 12:40:01
您无需在iOS中使用库进行JSON序列化.从iOS 5.0开始,可以使用类 NSJSONSerialization 来执行此操作. https://developer.apple.com/library /ios/documentation/foundation/reference/nsjsonserialization_class/Reference/Reference.html
You don't have to use a library for JSON Serialization in iOS. From iOS 5.0 onwards there is a class NSJSONSerialization is available for doing this. https://developer.apple.com/library/ios/documentation/foundation/reference/nsjsonserialization_class/Reference/Reference.html
考虑以下代码,将联系人列表转换为json字符串
Consider this code for converting list of contacts to json string
+(NSString*) jsonStringRepresentationOfSelectedReceiptients:(NSArray*) contactsList {
NSMutableArray* contactsArray = [[NSMutableArray alloc]init];
for (ContactObject *contactObj in contactsList) {
NSDictionary* ltPair = @{@"phone_no": [NSNumber numberWithLongLong:contactObj.phoneNo, @"email_id": contactObj.emailId, @"display_name": contactObj.displayName]};
[contactsArray addObject:ltPair];
}
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:contactsArray options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json string: %@", recptJson);
return jsonString;
}
希望这会有所帮助. (我给ios一个具体的答案,因为标签与iOS相关.
Hope this helps. (I gave ios specific answer because tags are related to iOS.