解析 JSON - SBJsonParser 的问题

发布于 2024-12-12 11:44:16 字数 2836 浏览 0 评论 0 原文

我目前在使用 SBJsonParser 解析一些 JSON 时遇到一个奇怪的问题。

现在,我正在解析的 JSON 如下。

[
   {
      "Company":{
         "id":"1",
         "company_name":null
      },
      "relations":{
         "store":[
            {
               "id":"1",
               "restaurant_name":"Dubai",
               "brief_description":null
            },
            {
               "id":"2",
               "restaurant_name":"Test2",
               "brief_description":null
            }
         ]
      }
   }
]

我可以轻松创建一个 NSDictionary 并用 Company node(?) 的正确信息填充它。

但是当涉及到关系存储节点时,我的问题就出现了。

 NSDictionary *relations = [object valueForKey:@"relations"];
 NSArray *multipleStores = [relations valueForKey:@"store"];
 NSLog(@"relations: %@", relations);

 for (NSDictionary *singleStore in multipleStores){

        NSLog(@"Single Store: %@", singleStore);

        [company grabStoreInformation:singleStore];

 }

这是上面 NSLog 的返回内容。

 relations: (
    {
    store =         (
                    {
            "brief_description" = "<null>";
            id = 1;
            "restaurant_name" = Dubai;
        },
                    {
            "brief_description" = "<null>";
            id = 2;
            "restaurant_name" = Test2;
        }
    );
 }
)

现在,如果不是 NSLog 中发生的事情,那就没问题了。看来 singleStore 实际上并不是获取单个存储节点,而是将两个存储节点添加在一起。

Single Store: (
    {
    "brief_description" = "<null>";
    id = 1;
    "restaurant_name" = Dubai;
    },
    {
    "brief_description" = "<null>";
    id = 2;
    "restaurant_name" = Test2;
    }
)

问题是,我需要将每个存储节点添加到 NSMutableArray 中。因此,NSDictionary 将被添加到 NSMutableArray 中,然后可以在其他地方访问(对于 UITableView 数据源)。

任何帮助将存储节点分开的帮助将不胜感激。

编辑 根据要求,用于解析的整个代码:

        SBJsonParser *parser = [[SBJsonParser alloc] init];

        // parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
        NSDictionary *object = [parser objectWithString:[response asString] error:nil];

        [parser release];

        NSDictionary *companyDetails = [object valueForKey:@"Company"];

        MACompany *company = [MACompany sharedMACompany];
        [company initWithDetails:companyDetails];

        NSDictionary *relations = [object valueForKey:@"relations"];
        NSArray *multipleStores = [relations valueForKey:@"store"];

        NSLog(@"relations: %@", relations);

        for (NSDictionary *singleStore in multipleStores){

            NSLog(@"Single Store: %@", singleStore);

            [company grabStoreInformation:singleStore];

        }

如您所见,我依靠单例类来复制 JSON 的元素。我认为,这与我在解决如何拆分单个存储字典时想要实现的目标无关。

I'm currently experiencing a weird issue when it comes to parsing some JSON when using the SBJsonParser.

Now, the JSON that I am parsing is as follows.

[
   {
      "Company":{
         "id":"1",
         "company_name":null
      },
      "relations":{
         "store":[
            {
               "id":"1",
               "restaurant_name":"Dubai",
               "brief_description":null
            },
            {
               "id":"2",
               "restaurant_name":"Test2",
               "brief_description":null
            }
         ]
      }
   }
]

I can easily create an NSDictionary and fill it with the correct information for the Company node(?).

But my issue arises when it comes to the relations and the store nodes.

 NSDictionary *relations = [object valueForKey:@"relations"];
 NSArray *multipleStores = [relations valueForKey:@"store"];
 NSLog(@"relations: %@", relations);

 for (NSDictionary *singleStore in multipleStores){

        NSLog(@"Single Store: %@", singleStore);

        [company grabStoreInformation:singleStore];

 }

Here is what the NSLog's above return.

 relations: (
    {
    store =         (
                    {
            "brief_description" = "<null>";
            id = 1;
            "restaurant_name" = Dubai;
        },
                    {
            "brief_description" = "<null>";
            id = 2;
            "restaurant_name" = Test2;
        }
    );
 }
)

Now this would be fine if it weren't for what was happening in the NSLog. It seems that singleStore isn't actually getting the individual store nodes, but adding both of the store nodes together.

Single Store: (
    {
    "brief_description" = "<null>";
    id = 1;
    "restaurant_name" = Dubai;
    },
    {
    "brief_description" = "<null>";
    id = 2;
    "restaurant_name" = Test2;
    }
)

The issue is, I need each store node to be added into an NSMutableArray. So the NSDictionary will then be added into an NSMutableArray and then accessible elsewhere (for a UITableView data source).

Any help would be greatly appreciated in getting the store nodes separate.

EDIT
As asked for, entire code for parsing:

        SBJsonParser *parser = [[SBJsonParser alloc] init];

        // parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
        NSDictionary *object = [parser objectWithString:[response asString] error:nil];

        [parser release];

        NSDictionary *companyDetails = [object valueForKey:@"Company"];

        MACompany *company = [MACompany sharedMACompany];
        [company initWithDetails:companyDetails];

        NSDictionary *relations = [object valueForKey:@"relations"];
        NSArray *multipleStores = [relations valueForKey:@"store"];

        NSLog(@"relations: %@", relations);

        for (NSDictionary *singleStore in multipleStores){

            NSLog(@"Single Store: %@", singleStore);

            [company grabStoreInformation:singleStore];

        }

As you can see I rely on a singleton class to copy elements of the JSON. This, I don't think, is relevant to what I am trying to achieve when it comes to working out how to split the single store dictionary.

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

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

发布评论

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

评论(1

雨的味道风的声音 2024-12-19 11:44:16

首先,请注意您的顶级元素是一个数组,而不是一个对象。因此:

NSDictionary *object = [parser objectWithString:[response asString] error:nil];

应该是:

NSArray *object = [parser objectWithString:[response asString] error:nil];

我会使用 companies 而不是 object 作为变量名称。

其次,使用-valueForKey:时必须小心。当发送到数组时,它返回另一个数组,其中包含与键参数对应的值。除非您熟悉键值编码,否则请使用 NSDictionary 中的规范方法返回与给定键关联的对象:-objectForKey:

如果您使用 -objectForKey: 而不是 -valueForKey:,您在运行程序时就会注意到这个结构错误。

记住这两点,您可以按如下方式浏览 JSON 数据:

NSArray *companies = [parser objectWithString:[response asString] error:nil];
for (NSDictionary *company in companies) {
    NSDictionary *companyDetails = [company objectForKey:@"Company"];
    NSDictionary *relations = [company objectForKey:@"relations"];
    NSArray *stores = [relations objectForKey:@"store"];

    for (NSDictionary *store in stores) {
        NSString *restaurantName = [store objectForKey:@"restaurant_name"];
        …
    }

我建议阅读 Stack Overflow 上的另一个问题:Difference valueforKey objectForKey< /a> 和 -valueForKey: 之前先阅读 rel="nofollow noreferrer">键值编码编程指南

Firstly, note that your top-level element is an array, not an object. Hence:

NSDictionary *object = [parser objectWithString:[response asString] error:nil];

should be:

NSArray *object = [parser objectWithString:[response asString] error:nil];

and I’d use companies instead of object as the variable name.

Secondly, you must be careful when using -valueForKey:. When sent to an array, it returns another array containing the values corresponding to the key argument. Unless you are familiar with Key-Value Coding, use the canonical method in NSDictionary to return the object associated to a given key: -objectForKey:.

Had you used -objectForKey: instead of -valueForKey:, you would have noticed this structural error when running your program.

With those two points in mind, you can navigate your JSON data as follows:

NSArray *companies = [parser objectWithString:[response asString] error:nil];
for (NSDictionary *company in companies) {
    NSDictionary *companyDetails = [company objectForKey:@"Company"];
    NSDictionary *relations = [company objectForKey:@"relations"];
    NSArray *stores = [relations objectForKey:@"store"];

    for (NSDictionary *store in stores) {
        NSString *restaurantName = [store objectForKey:@"restaurant_name"];
        …
    }

I recommend reading this other question on Stack Overflow: Difference valueforKey objectForKey and the Key-Value Coding Programming Guide before using -valueForKey:.

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