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 ノード(?) の正しい情報を入力できます。

しかし、リレーションストアノードに関しては、私の問題が発生します。

 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 の要素をコピーするためにシングルトン クラスに依存しています。これは、単一の店舗辞書を分割する方法を考え出すときに、私が達成しようとしていることに関連しているとは思いません。

4

1 に答える 1

1

まず、最上位の要素はオブジェクトではなく配列であることに注意してください。したがって:

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

する必要があります:

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

変数名のcompanies代わりに使用します。object

次に、を使用するときは注意が必要です-valueForKey:。配列に送信されると、キー引数に対応する値を含む別の配列を返します。Key-Valueコーディングに精通していない限り、で正規メソッドを使用しNSDictionaryて、指定されたキーに関連付けられたオブジェクトを返します-objectForKey:

-objectForKey:の代わりにを使用-valueForKey:した場合、プログラムの実行時にこの構造エラーに気付くでしょう。

これらの2つのポイントを念頭に置いて、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: DifferencevalueforKeyobjectForKeyとKey- ValueCodingプログラミングガイドに関するこの他の質問を読むことをお勧めします。-valueForKey:

于 2011-10-28T03:50:18.847 に答える