从 NSArray 创建一个 json 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17809929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create a json string from NSArray
提问by revolutionkpi
In my iPhone aplication I have a list of custom objects. I need to create a json string from them. How I can implement this with SBJSON or iPhone sdk?
在我的 iPhone 应用程序中,我有一个自定义对象列表。我需要从它们创建一个 json 字符串。我如何使用 SBJSON 或 iPhone sdk 实现这一点?
NSArray* eventsForUpload = [app.dataService.coreDataHelper fetchInstancesOf:@"Event" where:@"isForUpload" is:[NSNumber numberWithBool:YES]];
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *actionLinksStr = [writer stringWithObject:eventsForUpload];
and i get empty result.
我得到空结果。
回答by Thilina Chamin Hewagama
This process is really simple now, you don't have to use external libraries, Do it this way, (iOS 5 & above)
这个过程现在真的很简单,你不必使用外部库,这样做,(iOS 5及以上)
NSArray *myArray;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
回答by Damo
I love my categories so I do this kind of thing as follows
我喜欢我的类别所以我做这样的事情如下
@implementation NSArray (Extensions)
- (NSString*)json
{
NSString* json = nil;
NSError* error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return (error ? nil : json);
}
@end
回答by Rimon
Although the highest voted answer is valid for an array of dictionaries or other serializable objects, it's not valid for custom objects.
尽管投票最高的答案对字典数组或其他可序列化对象有效,但对自定义对象无效。
Here is the thing, you'll need to loop through your array and get the dictionary representation of each object and add it to a new array to be serialized.
事情就是这样,您需要遍历数组并获取每个对象的字典表示并将其添加到要序列化的新数组中。
NSString *offersJSONString = @"";
if(offers)
{
NSMutableArray *offersJSONArray = [NSMutableArray array];
for (Offer *offer in offers)
{
[offersJSONArray addObject:[offer dictionaryRepresentation]];
}
NSData *offersJSONData = [NSJSONSerialization dataWithJSONObject:offersJSONArray options:NSJSONWritingPrettyPrinted error:&error];
offersJSONString = [[NSString alloc] initWithData:offersJSONData encoding:NSUTF8StringEncoding] ;
}
As for the dictionaryRepresentation method in the Offer class:
至于Offer类中的dictionaryRepresentation方法:
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict setValue:self.title forKey:@"title"];
return [NSDictionary dictionaryWithDictionary:mutableDict];
}
回答by Mohammad Kamran Usmani
Try like this Swift 2.3
试试这个 Swift 2.3
let consArray = [1,2,3,4,5,6]
var jsonString : String = ""
do
{
if let postData : NSData = try NSJSONSerialization.dataWithJSONObject(consArray, options: NSJSONWritingOptions.PrettyPrinted)
{
jsonString = NSString(data: postData, encoding: NSUTF8StringEncoding)! as String
}
}
catch
{
print(error)
}
回答by Stig Brautaset
回答by Venk
Try like this,
试试这样,
- (NSString *)JSONRepresentation {
SBJsonWriter *jsonWriter = [SBJsonWriter new];
NSString *json = [jsonWriter stringWithObject:self];
if (!json)
[jsonWriter release];
return json;
}
then call this like,
然后这样称呼,
NSString *jsonString = [array JSONRepresentation];
Hope it will helps you...
希望能帮到你...

