2013-07-13 65 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”。 –