YAJL - iOS/iPhone 上的 JSON

发布于 2024-12-03 06:34:47 字数 956 浏览 1 评论 0原文

在我的 iPhone 应用程序中,我尝试使用 JSON 库 (YAJL) 创建一个类似于以下格式的 JSON 字符串:

{"user": 
  {"name":"Jon", "username":"jon22", "password":"passw@rd", "email":"[email protected]"}
}

但我无法弄清楚创建此字符串的 YAJL 方法。

我已尝试以下操作:

 NSArray *params = [NSArray arrayWithObjects:@"Jon", @"jon22", @"passw@rd", @"[email protected]", nil];
 NSArray *keys = [NSArray arrayWithObjects:@"name", @"username", @"password", @"email", nil];

 NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:params forKeys:keys];
 NSString *JSONString = [userDictionary yajl_JSONString];

但是,返回的字符串未包装在外部“用户”中。 如何使用 YAJL 创建这个 json 字符串? 有人有这方面的经验吗???

非常感谢, 布雷特

In my iPhone app, I am trying to use the JSON library (YAJL) to create a JSON string that looks like the following format:

{"user": 
  {"name":"Jon", "username":"jon22", "password":"passw@rd", "email":"[email protected]"}
}

But I can't figure out the YAJL methods to create this.

I have tried the following:

 NSArray *params = [NSArray arrayWithObjects:@"Jon", @"jon22", @"passw@rd", @"[email protected]", nil];
 NSArray *keys = [NSArray arrayWithObjects:@"name", @"username", @"password", @"email", nil];

 NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:params forKeys:keys];
 NSString *JSONString = [userDictionary yajl_JSONString];

However, the returned string is not wrapped in the outside "user".
How can I use YAJL to create this json string? Does anyone have experience with this???

Many thanks,
Brett

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

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

发布评论

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

评论(2

夜血缘 2024-12-10 06:34:47

我不使用 YAJL,而是使用 SBJSON,苹果也在 iOS 中使用它。 ..

无论如何,库的行为正确:您没有创建“用户”字典!

你需要做一些类似的事情:

NSDictionary *serialize = [NSDictionary dictionaryWithObject:userDictionary forKey:@"user"];

然后“JSON-ize”这个。

这是因为当您在 userDictionary 上调用 -yajl_JSONString 时,您正在使用 userDictionary 作为“根对象”。如果你想将它包装在另一个字典中,你需要明确地这样做。

I don't use YAJL, but SBJSON, which is the one Apple uses in iOS too...

Anyway, the library is behaving correctly: you're not creating an "user" dictionary!

You need to do something like:

NSDictionary *serialize = [NSDictionary dictionaryWithObject:userDictionary forKey:@"user"];

And then "JSON-ize" this one.

This is because when you call -yajl_JSONString on userDictionary, you are using userDictionary as your "root object". If you want to wrap this inside another dictionary, you need to explicitly do so.

愁以何悠 2024-12-10 06:34:47

我强烈建议您查看 JSONKit https://github.com/johnezang/JSONKit 我已经使用了很多JSON 解析器并发现它是最简单的。

文档也很棒。

I highly recommend that you check out JSONKit https://github.com/johnezang/JSONKit I have used numerous JSON parsers and have found it to be the easiest.

Also the documentation is awesome.

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