我需要帮助解析php上的json url
问题描述:
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;
?>
以下是json内容: p>
{
“ 成功“:true,
”数据“:”{\“campaign_name \”:\“helloworld \”,\“download_link \”:\“https:\\\ / \\\ / google.com \\\ / ACCESSKEY \\\ / GETFILE \\\ / M-spqn-e61-2aef2575a0b5250354f2b0fda033e703标记= HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&安培; DCMA = 5ecceb0522bcd0db \ “\ ”链接\?“:\” HTTP:\\\ / \\\ / www.lol。 com \\\ / remove \\\ / remove.php \“}”,
“message”:null
}
code> pre>
我正在寻找 获取download_link的值。 我该怎么做? p>
这就是我的尝试: p>
&lt;?php
$ jsndata = {“success” :真 “数据”: “{\” CAMPAIGN_NAME \ “:\” 的HelloWorld \ “\ ”DOWNLOAD_LINK \“:\” HTTPS:\\\ / \\\ / google.com \\\ / ACCESSKEY \\\ / GETFILE \\\ / M-spqn-e61-2aef2575a0b5250354f2b0fda033e703标记= HUSYjdC5jyJskXUHiKn13l1A1BaAjH2R&安培; DCMA = 5ecceb0522bcd0db \ “\ ”链接\?“:\” HTTP:\\\ / \\\ / www.lol.com \\\ /remove\\\/remove.php\"}","message":null};
$jsn = json_decode($ jsndata,true);
$ temperatureMin = $ jsn ['data'] [6 ] ['download_link'];
echo $ temperatureMin;
?&gt;
code> pre>
div>
答
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
答
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'];