2012-06-21 17 views
2

我試圖從nsdata類中獲取值並且不起作用。如何從xcode 4.2中的nsstring或byte獲取JSON數組數據?

這裏是我的JSON數據。

{ 
    "count": 3, 
    "item": [{ 
     "id": "1", 
     "latitude": "37.556811", 
     "longitude": "126.922015", 
     "imgUrl": "http://175.211.62.15/sample_res/1.jpg", 
     "found": false 
    }, { 
     "id": "3", 
     "latitude": "37.556203", 
     "longitude": "126.922629", 
     "imgUrl": "http://175.211.62.15/sample_res/3.jpg", 
     "found": false 
    }, { 
     "id": "2", 
     "latitude": "37.556985", 
     "longitude": "126.92286", 
     "imgUrl": "http://175.211.62.15/sample_res/2.jpg", 
     "found": false 
    }] 
} 

,這裏是我的代碼

-(NSDictionary *)getDataFromItemList 
{ 

    NSData *dataBody = [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)]; 
    NSDictionary *iTem = [[NSDictionary alloc]init]; 
    iTem = [NSJSONSerialization JSONObjectWithData:dataBody options:NSJSONReadingMutableContainers error:nil]; 
    NSLog(@"id = %@",[iTem objectForKey:@"id"]); 

    //for Test 
    output = [[NSString alloc] initWithBytes:buffer length:rangeHeader.length encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",output); 
    return iTem; 

} 

我怎麼能在JSON訪問的每一個值?請幫幫我。

+0

什麼不起作用? 'buffer'從哪裏來? – Mundi

+0

如果你正在解析JSON,爲什麼不使用SBJsonParser。它將以簡單的數據結構幫助您獲得所需的一切。或者是否有任何問題中您缺少的一點。 –

+0

緩衝區來自inputStream。我想從緩衝區獲取數據來自服務器,類型爲JSON,如上所示。我不想使用任何其他庫。我正在使用ARC,有時ARC不允許將庫轉換爲ARC代碼。 – user1471568

回答

4

這個樣子的..

NSString *jsonString = @"your json"; 
NSData *JSONdata = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *jsonError = nil; 
if (JSONdata != nil) { 
    //this you need to know json root is NSDictionary or NSArray , you smaple is NSDictionary 
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:JSONdata options:0 error:&jsonError]; 
    if (jsonError == nil) { 
     //every need check value is null or not , json null like ("count": null) 
     if (dic == (NSDictionary *)[NSNull null]) { 
      return nil; 
     } 

     //every property you must know , what type is 

     if ([dic objectForKey:@"count"] != [NSNull null]) { 
      [self setCount:[[dic objectForKey:@"count"] integerValue]]; 
     } 
     if ([dic objectForKey:@"item"] != [NSNull null]) { 
      NSArray *itemArray = [dic objectForKey:@"item"]; // check null if need 
      for (NSDictionary *itemDic in itemArray){ 
       NSString *_id = [dic objectForKey:@"id"]; // check null if need 
       NSNumber *found = (NSNumber *)[dic objectForKey:@"found"]; 
       //..... 
       //.... just Dictionary get key value 
      } 

     } 
    } 
} 
1

我做到了通過使用框架:http://stig.github.com/json-framework/

這是非常強大的,可以做令人難以置信的東西!

在這裏,我用它來從HTTP請求中提取項目名稱: (其中,結果是JSO字符串)

NSString *result = request.responseString; 

jsonArray = (NSArray*)[result JSONValue]; /* Convert the response into an array */ 

NSDictionary *jsonDict = [jsonArray objectAtIndex:0]; 

/* grabs information and display them in the labels*/ 
name = [jsonDict objectForKey:@"wine_name"]; 

希望這將是有益的

+0

你從哪裏得到'@「wine_name」從? – Mundi

+0

這是我的JSON字符串{「wine_name」中的一個關鍵字:「一些葡萄酒的名字」} – Edelweiss

+0

但這與問題有什麼關係?他已經從JSON中正確提取了NSDictionary ...沒關係。 – Mundi

1

看你的JSON,你不在查詢對象層次結構中的正確對象。您正確提取的頂部對象是NSDictionary。要獲取items數組和單個項目,您必須執行此操作。

NSArray *items = [iTem objectForKey:@"item"]; 
NSArray *filteredArray = [items filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat:@"id = %d", 2]; 
if (filteredArray.count) NSDictionary *item2 = [filteredArray objectAtIndex:0]; 
+0

我試過這個,但不起作用。它仍然會打印日誌(空) – user1471568

+0

其他日誌語句如何?追溯日誌並找出什麼是'nil'。也許你的輸入字符串?這是你將如何找到錯誤。 – Mundi

1

嘗試JSONKit這一點。使用起來非常簡單。

0

請注意,如果這仍然是相關的,但在iOS 5中,蘋果增加了對JSON的合理支持。退房this blog for a small Tutorial 無需導入任何JSON框架。 (+1如果這個答案是相關的)