使用JSON-Framework将NSMutableArray作为JSON发送 [英] Send NSMutableArray as JSON using JSON-Framework
问题描述
我在我的项目中成功使用JSON-Framework来解码服务器发送的JSON。
I'm using JSON-Framework in my project successfully to decode JSON send from a server.
现在我需要反过来这样做,我' m面临问题,因为要发送的数据是从CoreData获取的NSMutableArray。
Now I need to do it the other way around and I'm facing problems as the data to be sent is a NSMutableArray fetched from CoreData.
使用时
NSString* jsonString = [menuItems JSONRepresentation]
我收到消息JSON MenuItems不支持序列化。
I get the message "JSON serialisation not supported for MenuItems".
我是否需要将NSMutableArray转换为其他格式,以便JSON-Framework可以序列化它?
Do I need to convert the NSMutableArray to some other format so the JSON-Framework can serialize it?
感谢您的帮助,
Miguel
Thanks for any help,
Miguel
推荐答案
我终于解决了它,但我不确定这是否是最佳/最优雅的方式。
I finally solved it, but I'm not sure if it's the best/most elegant way to do it.
NSMutableArray* items = [[NSMutableArray alloc] init];
for (MenuItems* item in menuItems) {
[items addObject:[NSArray arrayWithObjects:item.id,[item.modified description],nil]];
}
NSString *post = [NSString stringWithFormat:@"currentData=%@",
[items JSONRepresentation]];
说明:
我首先想到的问题是NSMutableArray,但是意识到这是它的内容。所以我只是从中得到了我需要的信息,并将其保存为JSON-Framework接受的NSArray: - )
Explanation:
I first thought that the problem was the NSMutableArray, but then realized that it was the contents of it. So I just get the information I need out of it and saved it as NSArray which JSON-Framework does accept :-)
这篇关于使用JSON-Framework将NSMutableArray作为JSON发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!