从“JSON风格”获取正确的值Objective-C 中的 NSString

发布于 2024-11-06 19:35:56 字数 375 浏览 0 评论 0原文

我试图从下面的 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 技术交流群。

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

发布评论

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

评论(2

我认为这不是格式良好的 JSON。一方面,这将返回一个数组..(被 [] 包围,而不是 {})。另外,您的数组有空条目(“,”,没有任何内容分隔它们)

您使用 SBJSON 吗?

如果您查阅 NSObject+JSON.h,您将看到此注释:

/// Returns the NSDictionary or NSArray represented by the receiver's JSON representation, or nil on error

存在错误。检查控制台输出,您应该会看到那里发布的错误。

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:

/// Returns the NSDictionary or NSArray represented by the receiver's JSON representation, or nil on error

There has been an error. Check your console output, you should see an error posted there.

等往事风中吹 2024-11-13 19:35:56

我有点不确定您发布的数组如何是 JSON 样式的 NSString,但我相信您返回的 NSDictionary 基本上应该存储与 JSON 原始文件中出现的键值对完全相同的键值对。例如,如果您有如下 JSON:

{key: "value"}

那么您将获得一个 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:

{key: "value"}

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?

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