2013-07-13 100 views
-4

DATA:如何在iOS中獲取json數據?

2 =  { 
     "delivery_cost" = "Free Delivery"; 
     "delivery_cost_aed" = "0 DHs"; 
     "delivery_time" = "24 hours"; 
     "payment_modes" = "[{\"icon\":\"216.70.96.219\\/_media\\/images\\/ico\\/ico_pm_cod.gif\",\"label\":\"Cash On Delivery\"},{\"icon\":\"216.70.96.219\\/_media\\/images\\/ico\\/ico_pm_cod.gif\",\"label\":\"Cash On Delivery\"}]"; 
     "product_price" = "1,400 DHs"; 
     "product_url" = "http://www.mygsm.me/Htc-Flyer-16GB.html"; 
     "shop_id" = "o2md-0qwq-9qam-y5m1"; 
     "shop_logo" = "216.70.96.219/_media/images/shop_logos/ShopLogos_07.gif"; 
     "shop_url" = "mygsm.me"; 
     shopname = "Mygsm.me"; 
     stock = 1; 
     "total_cost" = "1,400 DHs"; 
     warranty = "216.70.96.219/_media/images/ico/warranty/1.png"; 
    }; 

獲得價值:

我想從payment_modes在陣列得到所有圖標網址。像這樣

216.70.96.219 \/_媒體\ /影像\/ICO \ /ico_pm_cod.gif

216.70.96.219 \/_媒體\ /影像\/ICO \ /ico_pm_cod.gif

+1

我想要錢。 – 2013-07-13 10:30:11

+1

@ChiragDj你有很大的答案GOOGLE和SO。 –

+0

@ChiragDj爲什麼downVote我的其他答案? –

回答

1

你想去玩NSJSONSerialization類。你的代碼看起來像這樣:

NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:responseData 
          options:kNilOptions 
          error:&error]; 

    NSArray* commands = [json objectForKey:@"foo"]; 

請注意,我在我的JSON中有一個數組,但它可能很容易就是一個字典。

+1

OP已經將JSON解析爲NSDictionary,從轉儲列表中可以看出。 –

+0

是的。鑑於OP投入到這個問題上的努力很少,我在答案中做出了同樣微小的努力。我認爲指出NSJSONSerialization類可能已經足夠,但將lmgtfy鏈接放在一個有點厚顏無恥的位置......: - ) –

0

要轉換一個JSON字符串Foundation對象

NSString *jsonString = ...; 
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonString options:0 error:nil]; 
NSArray *payment_modes = [dict objectForKey:@"payment_modes"]; 
+0

外部字典元素的關鍵字爲「2」。 –