我在我的项目中成功地使用了JSON-Framework来解码来自服务器的JSON发送。
现在我需要反其道而行之,我面临着一些问题,因为要发送的数据是从CoreData获取的NSMutableArray。
在使用时
NSString* jsonString = [menuItems JSONRepresentation]我收到消息“MenuItems不支持JSON序列化”。
我是否需要将NSMutableArray转换为其他格式,以便JSON-Framework可以序列化它?
谢谢你的帮助
米格尔
发布于 2010-07-14 19:09:48
我最终解决了它,但我不确定这是否是最好/最优雅的方法。
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上,但后来意识到问题出在它的内容上。因此,我只是从中获取所需的信息,并将其另存为NSArray,这是JSON框架所接受的:-)
发布于 2010-09-22 04:52:57
请允许我建议一个更好的解决方案:
在MenuItems类中,实现-proxyForJson方法,然后您应该能够直接在menuItems数组上调用-JSONRepresentation方法。
@interface MenuItems(SBJson)
-(id)proxyForJson {
return [NSDictionary dictionaryWithObjectsAndKeys:
self.id,@"id",
[self.modified description],@"modified",
nil];
}
@end希望这能有所帮助!
发布于 2011-07-19 00:50:17
这是一个将字典和数组发送到server.which的示例,我的工作效率为1000000。
SBJSON *jparser = [[SBJSON new] autorelease];
NSString *ArrayjsonItems = [jparser stringWithObject:self.UrMergedArray];
NSString *DicjsonItems = [jparser stringWithObject:self.UrMergedDic];
NSLog(@"array Items :%@",self.UrMergedArray);
NSLog(@"dic Items :%@",self.UrMergedDic);
NSString *postString =[NSString stringWithFormat:@"Arrayitems=%@&Dicitems=%@",ArrayjsonItems,DicjsonItems];
NSLog(@"it is going to post : %@ \n\n",postString);
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:snapURL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
if (connection) {
self.receivedData = [[NSMutableData alloc]init];
}https://stackoverflow.com/questions/3245191
复制相似问题