简体   繁体   中英

i need help on parsing json url on php

Here is the json content:

{
   "success":true,
    "data":"{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"}",
    "message":null
}

I'm looking to get the value of download_link. How can I do that?

This is what I tried:

<?php
$jsndata = {"success":true,"data":"{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"}","message":null};
$jsn = json_decode($jsndata,true);

$temperatureMin = $jsn['data'][6]['download_link'];
echo $temperatureMin;
 ?>

You need:

- quotes around your JSON
- to remove the quotes around the inner object

$jsndata = '{"success":true,"data":{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"},"message":null}' ;

And you need to remove the [6] from your array access:

$temperatureMin = $jsn['data']['download_link'];

You have a json object containing another serialized json object.

Decode the first object, get the second serialized object and decode that:

$jsndata = '{
    "success":true,
    "data":"{\"campaign_name\":\"helloworld\",\"download_link\":\"https:\\\/\\\/google.com\\\/accesskey\\\/getfile\\\/m-spqn-e61-2aef2575a0b5250354f2b0fda033e703?token=HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&dcma=5ecceb0522bcd0db\",\"link\":\"http:\\\/\\\/www.lol.com\\\/remove\\\/remove.php\"}",
    "message":null
}';

// Decode the main json object
$jsn = json_decode($jsndata,true);

// Since 'data' is another serialized object, you need to decode that as well:
$data = json_decode($jsn['data'], true);

// Now you can access the contents of 'data'
echo $data['download_link'];

Demo: https://3v4l.org/1PcQp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM