如何在iOS中获取json数据?
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
你想去玩NSJSONSerialization类。你的代码看起来像这样:
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* commands = [json objectForKey:@"foo"];
请注意,我在我的JSON中有一个数组,但它可能很容易就是一个字典。
OP已经将JSON解析为NSDictionary,从转储列表中可以看出。 –
是的。鉴于OP投入到这个问题上的努力很少,我在答案中做出了同样微小的努力。我认为指出NSJSONSerialization类可能已经足够,但将lmgtfy链接放在一个有点厚颜无耻的位置......: - ) –
要转换一个JSON字符串Foundation对象
NSString *jsonString = ...;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonString options:0 error:nil];
NSArray *payment_modes = [dict objectForKey:@"payment_modes"];
外部字典元素的关键字为“2”。 –
我想要钱。 – 2013-07-13 10:30:11
@ChiragDj你有很大的答案GOOGLE和SO。 –
@ChiragDj为什么downVote我的其他答案? –