YAJL - iOS/iPhone 上的 JSON
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不使用 YAJL,而是使用 SBJSON,苹果也在 iOS 中使用它。 ..
无论如何,库的行为正确:您没有创建“用户”字典!
你需要做一些类似的事情:
然后“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:
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.我强烈建议您查看 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.