从NSArray创建一个json字符串 [英] create a json string from NSArray

查看:228
本文介绍了从NSArray创建一个json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我有一个自定义对象列表。我需要从它们创建一个json字符串。我如何用SBJSON或iPhone sdk实现这个?

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?

 NSArray* eventsForUpload = [app.dataService.coreDataHelper fetchInstancesOf:@"Event" where:@"isForUpload" is:[NSNumber numberWithBool:YES]];
    SBJsonWriter *writer = [[SBJsonWriter alloc] init];  
    NSString *actionLinksStr = [writer stringWithObject:eventsForUpload];

我得到空结果。

推荐答案

这个过程非常简单,您不必使用外部库,
这样做,(iOS 5及以上版本)

This process is really simple now, you don't have to use external libraries, Do it this way, (iOS 5 & above)

NSArray *myArray;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

这篇关于从NSArray创建一个json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆