从“JSON风格”获取正确的值Objective-C 中的 NSString
我试图从下面的 JSON 样式的 NSString 中获取 NSString“Bonjour”:
[[["Bonjour","Hello","",""]],,"en",,[["Bonjour",[4],1,0,1000,0,1,0]],[["Hello",4,[["Bonjour",1000,1,0]],[[0,5]],"Hello"]],,,,2]
实现以下代码给我 null,假设结果是上面的 NSString
NSDictionary *td = [result JSONValue];
NSLog(@"\n%@", td);
如何从这个 JSON 样式的字符串中获取“Bonjour”?
I am trying to get the NSString "Bonjour" from the JSON-style NSString below:
[[["Bonjour","Hello","",""]],,"en",,[["Bonjour",[4],1,0,1000,0,1,0]],[["Hello",4,[["Bonjour",1000,1,0]],[[0,5]],"Hello"]],,,,2]
Implementing the following code gives me null, assuming result is the NSString above
NSDictionary *td = [result JSONValue];
NSLog(@"\n%@", td);
How can I grab "Bonjour" from this JSON-style string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这不是格式良好的 JSON。一方面,这将返回一个数组..(被 [] 包围,而不是 {})。另外,您的数组有空条目(“,”,没有任何内容分隔它们)
您使用 SBJSON 吗?
如果您查阅 NSObject+JSON.h,您将看到此注释:
存在错误。检查控制台输出,您应该会看到那里发布的错误。
I don't think this is well-formed JSON. For one thing this would return an array.. (surrounded by [], not {}). Also, your arrays have empty entries (',' with nothing separating them)
Are you using SBJSON?
If you consult NSObject+JSON.h you'll see this comment:
There has been an error. Check your console output, you should see an error posted there.
我有点不确定您发布的数组如何是 JSON 样式的 NSString,但我相信您返回的 NSDictionary 基本上应该存储与 JSON 原始文件中出现的键值对完全相同的键值对。例如,如果您有如下 JSON:
那么您将获得一个 NSDictionary,其“key”键与“value”值匹配。
也许你的 NSDictionary 为 null 的原因是你没有向它传递 JSONValue 可以理解的东西?
I'm somewhat unsure how the array you posted is a JSON-style NSString, but I believe the NSDictionary you get back should basically store key value pairs exactly as they appeared in the JSON original. For example, if you had JSON like:
Then you'd get an NSDictionary with a "key" key matched to a value of "value".
Perhaps the reason your NSDictionary is null is because you aren't passing it something that
JSONValue
understands?