iPhone SDK:解析 JSON 时出现问题

发布于 2024-12-14 02:42:29 字数 757 浏览 0 评论 0原文

我正在为我的 iPhone 应用程序使用 SBJSONParser。到目前为止,我一直在解析简单的 json 字符串,例如: ["Business1","Business2"]

我现在使用 PHP 从数据库中获取企业名称和企业 ID相同的 json 字符串,所以我的 PHP 给了我这样的结果:

{"business_1A" : "ABC_1","businees_2A": "ABC_2" }

这是我当前用来处理第一个 JSON 输出的代码,效果很好:

businessNames 在以下代码中是一个 NSMutableArray。

NSString *businessNamesJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"businessNamesJSON.php"]]];

SBJsonParser *parser = [[SBJsonParser alloc]init];
businessNames = [[parser objectWithString:businessNamesJSON error:nil]copy];

基本上,我想拆分第二个 JSON 输出,以便可以拥有两个单独的 NSMutableArray,一个包含业务名称,另一个包含 ID。 如何提取或拆分第二个 JSON 输出以便我可以执行此操作?

提前致谢。

I'm using the SBJSONParser for my iphone app. Up to now, i've been parsing simple json strings such as: ["Business1","Business2"]

I'm now using PHP to get both the business name and business ID from the database within the same json string, so my PHP is giving me a result like this:

{"business_1A" : "ABC_1","businees_2A": "ABC_2" }

Here's the code that i'm currently using to process the first JSON output which works fine:

businessNames is an NSMutableArray in the following code.

NSString *businessNamesJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"businessNamesJSON.php"]]];

SBJsonParser *parser = [[SBJsonParser alloc]init];
businessNames = [[parser objectWithString:businessNamesJSON error:nil]copy];

Basically, I want to split the second JSON output so that I can have two separate NSMutableArrays, one which contains the business Names and the other which holds the IDs.
How do I extract or split the second JSON output so I can do this?

Thanks in advance.

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

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

发布评论

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

评论(2

_蜘蛛 2024-12-21 02:42:29

你好,

让我退一步。既然你有一个公司列表,那么用 json 数组表示你的数据不是更好的方法吗?如下所示:

[
    {
        "identifier": "ABC_1",
        "name": "business_1A"
    },
    {
        "identifier": "ABC_2",
        "name": "businees_2A"
    }
] 

我相信这会让你更轻松地解析数据,并且会允许您将来添加更多属性。

因此,一旦有了这个结构,您就可以解析 json 数据,然后循环遍历条目并分别提取键 identifiername (在本例中)的值。

Hy there

Let me take a step back. Since you have a list of companies wouldn't it be a better way to represent your data with an array in json like so:

[
    {
        "identifier": "ABC_1",
        "name": "business_1A"
    },
    {
        "identifier": "ABC_2",
        "name": "businees_2A"
    }
] 

I believe this would make the parsing of the data easier for you and it would allow you to add more attributes in the future.

So once you have this structure you can parse the json data and then loop over the entries and extract the values for the keys identifier and name (in this case) respectively.

魂ガ小子 2024-12-21 02:42:29

{"business_1A" : "ABC_1","businees_2A": "ABC_2" } 用 JSON 术语定义了一个对象,任何正常的 JSON 解析器都会将其作为 Objective-C 中的 NSDictionary 返回,即从一个对象到另一个对象的映射的集合。

那么您似乎想要将所有键和所有值分开。在这种情况下,您可以从 NSDictionary 中获取它们:

SBJsonParser *parser = [[SBJsonParser alloc] init];
businessNamesDictionary = [parser objectWithString:businessNamesJSON error:nil];

NSLog(@"names: %@", [businessNamesDictionary allKeys]);
NSLog(@"values: %@", [businessNamesDictionary allValues]);

如果需要,可以获取 mutableCopy 。如果您想保证值的出现顺序与键的顺序相同,请使用objectsForKeys:notFoundMarker: - 每个值的顺序在文档中明确未定义,因此不要依赖于您发生的任何顺序获取您碰巧测试的操作系统版本。

{"business_1A" : "ABC_1","businees_2A": "ABC_2" } defined an object in JSON terms, which will be returned by any sane JSON parser as an NSDictionary in Objective-C, being a collection of mappings from one object to another.

You seem then to want all the keys and all the values separately. In that case you can just get them from the NSDictionary:

SBJsonParser *parser = [[SBJsonParser alloc] init];
businessNamesDictionary = [parser objectWithString:businessNamesJSON error:nil];

NSLog(@"names: %@", [businessNamesDictionary allKeys]);
NSLog(@"values: %@", [businessNamesDictionary allValues]);

Take mutableCopys if you want them. Use objectsForKeys:notFoundMarker: if you want to guarantee that the values come out in the same order as the keys — the order of each is explicitly undefined in the documentation so don't rely on whatever order you happen to get on whichever version of the OS you happen to test against.

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