PDA

View Full Version : دریافت اطلاعات از JSON توسط PHP



siavashsay
چهارشنبه 20 اسفند 1393, 09:19 صبح
با سلام !
بنده توسط تابع cURL یک سری اطلاعات رو از یک آدرس میگیرم که اون اطلاعات توسط JSON داره به بنده بازگردونده میشه !
نمونه اطلاعات JSON داده شده به این شکل هست :

{ "links": [
{ "title": "Buy ", "description": "", "slug": "buy", "shorturl": "http://xxx.x", "url": "http://xxx.x", "cover_image": null, "currency": "USD", "base_price": "100.00", "quantity": null, "quantity_sold": 0, "requires_shipping": false, "ships_within_days": null, "start_date": null, "end_date": null, "venue": null, "timezone": null, "note": "", "redirect_url": "", "webhook_url": "", "status": "Live", "enable_pwyw": true, "enable_sign": false },


{ "title": "SFPP", "description": "", "slug": "buy-it", "shorturl": "http://xxx.x", "url": "http://xxx.x", "cover_image": null, "currency": "USD", "base_price": "1999.00", "quantity": null, "quantity_sold": 0, "requires_shipping": true, "ships_within_days": 3, "start_date": null, "end_date": null, "venue": null, "timezone": null, "note": "Thank you for your purchase! Get excited for rocking your dating life!", "redirect_url": "http://xxx.x", "webhook_url": "", "status": "Live", "enable_pwyw": false, "enable_sign": false }
],
"success": true
}
حالا بنده میخوام اطلاعات مربوط به هر کدوم رو دریافت کنم !
مثلا محتوی title ها رو میخوام بگیرم و در یک متغیر PHP بذارم !
چطور باید اینکارو بکنم !
متشکرم

hamedarian2009
چهارشنبه 20 اسفند 1393, 09:41 صبح
سلام با تابع json_decode مقادیر json رو بهش بدین و تبدیل به آرایه میشه و میتونید مثل یک آرایه باهاش برخورد کنید


$json = '{ "links": [
{ "title": "Buy ", "description": "", "slug": "buy", "shorturl": "http://xxx.x", "url": "http://xxx.x", "cover_image": null, "currency": "USD", "base_price": "100.00", "quantity": null, "quantity_sold": 0, "requires_shipping": false, "ships_within_days": null, "start_date": null, "end_date": null, "venue": null, "timezone": null, "note": "", "redirect_url": "", "webhook_url": "", "status": "Live", "enable_pwyw": true, "enable_sign": false },


{ "title": "SFPP", "description": "", "slug": "buy-it", "shorturl": "http://xxx.x", "url": "http://xxx.x", "cover_image": null, "currency": "USD", "base_price": "1999.00", "quantity": null, "quantity_sold": 0, "requires_shipping": true, "ships_within_days": 3, "start_date": null, "end_date": null, "venue": null, "timezone": null, "note": "Thank you for your purchase! Get excited for rocking your dating life!", "redirect_url": "http://xxx.x", "webhook_url": "", "status": "Live", "enable_pwyw": false, "enable_sign": false }
],
"success": true
}';

$jsonArray = json_decode($json, true);
print_r($jsonArray);

siavashsay
چهارشنبه 20 اسفند 1393, 09:53 صبح
حامد جان واقعا متشکرم !
نمیدونم من همه این راها رو تست کردم اما چرا جواب نداد !
اما همین 2 خط کدی که شما الان گذاشتی جواب داد !
واقعا من اینو هم امتحان کرده بودم ! شاید یه جا مشکل داشتم !
در هر صورت واقعا متشکرم !
لطف کردید

hamedarian2009
چهارشنبه 20 اسفند 1393, 10:16 صبح
خواهش میکنم دوست عزیز شاید true رو نزاشتی چون در اینصورت به صورت آبجکت بر میگردونه