2011-03-23 120 views
1

我試圖解析一個複雜的json(使用Json框架),問題是json有很多嵌套的對象。花了太多的時間編程,我找不到解決方案,我的代碼聞起來,我有很多崩潰。 有什麼想法? (我沒有任何問題使用另一個JSON框架,如jsonkit或其他)需要幫助來解析複雜的JSON

這裏是json的簡化版本。

[ 
{ 
    "category": { 
     "id": 451, 
     "description": "Product description", 
     "quantity": "3149", 
     "category": [ 
      { 
       "category": { 
        "id": 115, 
        "description": "Product description", 
        "quantity": "465", 
        "category": [ 
         { 
          "category": { 
           "id": 125, 
           "description": "Product description", 
           "quantity": "6" 
          } 
         }, 
         { 
          "category": { 
           "id": 120, 
           "description": "Product description", 
           "quantity": "57", 
           "category": [ 
            { 
             "category": { 
              "id": 193, 
              "description": "Product description", 
              "quantity": "27" 
             } 
            }, 
            { 
             "category": { 
              "id": 194, 
              "description": "Product description", 
              "quantity": "14" 
             } 
            } 
           ] 
          } 
         }, 
         { 
          "category": { 
           "id": 446, 
           "description": "Product description", 
           "quantity": "7" 
          } 
         }, 
         { 
          "category": { 
           "id": 132, 
           "description": "Product description", 
           "quantity": "28", 
           "category": [ 
            { 
             "category": { 
              "id": 224, 
              "description": "Product description", 
              "quantity": "3" 
             } 
            }, 
            { 
             "category": { 
              "id": 137, 
              "description": "Product description", 
              "quantity": "4" 
             } 
            } 
           ] 
          } 
         }, 
         { 
          "category": { 
           "id": 128, 
           "description": "Product description", 
           "quantity": "21" 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "category": { 
        "id": 21, 
        "description": "Product description", 
        "quantity": "225", 
        "category": [ 
         { 
          "category": { 
           "id": 170, 
           "description": "Product description", 
           "quantity": "1" 
          } 
         }, 
         { 
          "category": { 
           "id": 68, 
           "description": "Product description", 
           "quantity": "45", 
           "category": [ 
            { 
             "category": { 
              "id": 81, 
              "description": "Product description", 
              "quantity": "2" 
             } 
            }, 
            { 
             "category": { 
              "id": 69, 
              "description": "Product description", 
              "quantity": "2" 
             } 
            } 
           ] 
          } 
         } 
        ] 
       } 
      } 
     ] 
    } 
}, 
{ 
    "category": { 
     "id": 390, 
     "description": "Product description", 
     "quantity": "251", 
     "category": [ 
      { 
       "category": { 
        "id": 714, 
        "description": "Product description", 
        "quantity": "0" 
       } 
      }, 
      { 
       "category": { 
        "id": 622, 
        "description": "Product description", 
        "quantity": "22", 
        "category": [ 
         { 
          "category": { 
           "id": 192, 
           "description": "Product description", 
           "quantity": "16" 
          } 
         }, 
         { 
          "category": { 
           "id": 391, 
           "description": "Product description", 
           "quantity": "6" 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "category": { 
        "id": 612, 
        "description": "Product description", 
        "quantity": "5" 
       } 
      }, 
      { 
       "category": { 
        "id": 621, 
        "description": "Product description", 
        "quantity": "123", 
        "category": [ 
         { 
          "category": { 
           "id": 628, 
           "description": "Product description", 
           "quantity": "25" 
          } 
         }, 
         { 
          "category": { 
           "id": 457, 
           "description": "Product description", 
           "quantity": "3" 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "category": { 
        "id": 720, 
        "description": "Product description", 
        "quantity": "1" 
       } 
      }, 
      { 
       "category": { 
        "id": 188, 
        "description": "Product description", 
        "quantity": "52", 
        "category": [ 
         { 
          "category": { 
           "id": 437, 
           "description": "Product description", 
           "quantity": "10" 
          } 
         }, 
         { 
          "category": { 
           "id": 639, 
           "description": "Product description", 
           "quantity": "7" 
          } 
         }, 
         { 
          "category": { 
           "id": 152, 
           "description": "Product description", 
           "quantity": "4", 
           "category": [ 
            { 
             "category": { 
              "id": 34, 
              "description": "Product description", 
              "quantity": "3" 
             } 
            }, 
            { 
             "category": { 
              "id": 31, 
              "description": "Product description", 
              "quantity": "1" 
             } 
            } 
           ] 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "category": { 
        "id": 548, 
        "description": "Product description", 
        "quantity": "3" 
       } 
      } 
     ] 
    } 
}] 

我的目的是創建對象數組來填充數據庫,表看起來是這樣的:

idfather(父類)
idproduct
描述

時間戳

我試過的最後一件事:
+3

你沒有告訴我們你的問題到底是什麼細節。它在哪裏崩潰?是什麼原因?從json-framework返回的信息處理這些信息的代碼是什麼樣的?所有這些都是需要回答的有效問題,任何人都可以給你任何可靠的建議。 – jer 2011-03-23 15:43:13

+1

JSON示例不是非常複雜。任何JSON庫都應該能夠處理它。所以問題不在於解析JSON,問題可能在於你走過的結果對象。但是沒有一些我們永遠不會知道的代碼。 – 2011-03-23 16:02:02

+0

鍵值編碼呢?它基本上解決了我所有關於漫步JSON數據的問題。 – 2012-01-17 17:16:34

回答

1

你可以使用TouchJSON,它非常簡單並且有很好的表現。它將你的json作爲數據,並且可以在一行中返回一個NSArray或NSDictionnary。

如果你的JSON有一個列表結構:

NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:yourData error:nil]; 

否則:

NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:yourData error:nil; 

你一定要包括你想使用它的類項目中的ANS進口#import "CJSONDeserializer.h"TouchJSONhttps://github.com/TouchCode/TouchJSON

0

看起來您的問題可能是JSON數據結構中的列表。當我使用JSON框架時,我無法使用keypath訪問數組值。

我解決了服務器端的問題,不允許數據結構中的數組 - 只有鍵/值對。然後,我可以通過根節點的keypath訪問結構中的每個值。 (當我想將列表轉換爲JSON時,我只使用了從0開始的數字。)

你確定數據結構的語法是正確的嗎?也許你的框架無法處理它 - 儘管它應該能夠。我使用這個:https://github.com/stig/json-framework/