2013-06-30 50 views
0

是這樣的內容...解析JSON把所有的內容在一個NSArray中

[ 
    { 
     "id": "", 
     "title": "", 
     "website": "", 
     "categories": [ 
      { 
       "id": "", 
       "label": "" 
      } 
     ], 
     "updated": 
    }, 
    { 
     "id": "", 
     "title": "", 
     "website": "", 
     "categories": [ 
      { 
       "id": "", 
       "label": "" 
      } 
     ], 
     "updated": 
    } 
] 

我怎樣才能插入一個陣列中每一個的供應源?

NSDictionary *results = [string JSONValue]; 
NSArray *subs = [results valueForKey:@"KEY"]; 

我必須插入哪個鍵? 感謝

+1

你有一個字典數組。您必須以某種方式迭代數組以從字典中提取值。它非常簡單,只需查看結構並瞭解代碼必須與結構匹配。花5分鐘時間在json.org上學習JSON語法,以便理解它。 –

+0

當然,您已經編輯了上述JSON,使其不再有效。 –

回答

2

,因爲我可以看到你的結構,你會擺脫這種的JSON字符串

NSArray: 
[ 
    NSDictionary: 
    { 
     NSString: "id", 
     NSString: "title", 
     NSString: "website", 
     NSArray: "categories": 
     [ 
      NSDictionary: 
      { 
       NSString: "id", 
       NSString: "label" 
      } 
     ], 
     NSNumber: "updated" 
    }, 
    NSDictionary: 
    { 
     ... 
    } 
] 

所以,你已經數組「訂閱」的根,你必須與他們的索引itterate他們在數組中。對於第一個ID,即[[myJsonStructure objectAtIndex:0] objectForKey:@"id"];

+0

非常感謝你! –

+0

對於較新的編譯器版本,還應該提及新的語法:* myJsonStructure [0] [@「id」]; *。 –