iPhone SDK:解析 JSON 时出现问题
我正在为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你好,
让我退一步。既然你有一个公司列表,那么用 json 数组表示你的数据不是更好的方法吗?如下所示:
我相信这会让你更轻松地解析数据,并且会允许您将来添加更多属性。
因此,一旦有了这个结构,您就可以解析 json 数据,然后循环遍历条目并分别提取键
identifier
和name
(在本例中)的值。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:
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
andname
(in this case) respectively.{"business_1A" : "ABC_1","businees_2A": "ABC_2" }
用 JSON 术语定义了一个对象,任何正常的 JSON 解析器都会将其作为 Objective-C 中的 NSDictionary 返回,即从一个对象到另一个对象的映射的集合。那么您似乎想要将所有键和所有值分开。在这种情况下,您可以从 NSDictionary 中获取它们:
如果需要,可以获取 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
:Take
mutableCopy
s if you want them. UseobjectsForKeys: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.