使用 JSON-Framework 将 NSMutableArray 作为 JSON 发送

发布于 2024-09-09 21:31:50 字数 325 浏览 6 评论 0原文

我在项目中成功使用 JSON-Framework 来解码从服务器发送的 JSON。

现在我需要以相反的方式进行操作,并且我遇到了问题,因为要发送的数据是从 CoreData 获取的 NSMutableArray。

使用时,

NSString* jsonString = [menuItems JSONRepresentation]

我收到消息“菜单项不支持 JSON 序列化”。

我是否需要将 NSMutableArray 转换为其他格式以便 JSON 框架可以序列化它?

感谢您的帮助,
米格尔

I'm using JSON-Framework in my project successfully to decode JSON send from a server.

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.

When using

NSString* jsonString = [menuItems JSONRepresentation]

I get the message "JSON serialisation not supported for MenuItems".

Do I need to convert the NSMutableArray to some other format so the JSON-Framework can serialize it?

Thanks for any help,
Miguel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2024-09-16 21:31:50

请允许我建议一个更好的解决方案:

在您的 MenuItems 类中,实现 -proxyForJson 方法,然后您应该能够直接在 上调用 -JSONRepresentation 方法>menuItems 数组。

@interface MenuItems(SBJson)
-(id)proxyForJson {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            self.id,@"id",
            [self.modified description],@"modified",
            nil];
}
@end

希望这有帮助!

Allow me to suggest a somewhat nicer solution:

In your MenuItems class, implement the -proxyForJson method and you should then be able to call the -JSONRepresentation method directly on the menuItems array.

@interface MenuItems(SBJson)
-(id)proxyForJson {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            self.id,@"id",
            [self.modified description],@"modified",
            nil];
}
@end

Hope this helps!

自在安然 2024-09-16 21:31:50

我终于解决了它,但我不确定这是否是最好/最优雅的方法。

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 :-)

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]];

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 :-)

是伱的 2024-09-16 21:31:50

这是一个将字典和数组发送到服务器的示例。这对我有用 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];

}

This is an example of sending dictionary and array to server.which worked for me 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];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文