需要帮助从PHP中获取JSON的一部分

需要帮助从PHP中获取JSON的一部分

问题描述:

Need help in getting some part from my json. I already search for this on website and google but didn't find good example. This my json :

{
"_doc": "lusti",
"id": 123456,
"title": "Dokumenku dari Json",
"parsel": {
    "_doc": "parsel",
    "id": 3256,
    "doc_type": "Word",
    "title": "bendahara12.doc",
    "download_link": {
        "Word": [
            {
                "label": "doc1",
                "file": "http://example.com/file_doc1.doc"
            },
            {
                "label": "doc2",
                "file": "http://example.com/file_doc2.doc"
            },
            {
                "label": "doc3",
                "file": "http://example.com/file_doc3.doc"
            },
            {
                "label": "doc4",
                "file": "http://example.com/file_doc4.doc"
            }
        ]
    }
}

and this my table :

<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 1</a></td>
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 2</a></td>
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 3</a></td>
<td class="tg-baqh"><a class="btn" href="<?php echo $json['???']; ?>">Doc 4</a></td>

i need get link for my download button. look like :

<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc1.doc">Doc 1</a></td>
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc2.doc">Doc 2</a></td>
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc3.doc">Doc 3</a></td>
<td class="tg-baqh"><a class="btn" href="http://example.com/file_doc4.doc">Doc 4</a></td>

需要帮助从我的json中获取部分内容。 我已经在网站和谷歌搜索这个,但没有找到好的例子。 这是我的json: p>

  {
“_doc”:“lusti”,
“id”:123456,
“title”:“Dokumenku dari Json”,\  n“parsel”:{
“_doc”:“parsel”,
“id”:3256,
“doc_type”:“Word”,
“title”:“bendahara12.doc”,
“download_link  “:{
”Word“:[
 {
”label“:”doc1“,
”file“:”http://example.com/file_doc1.doc"
},
 {\  n“label”:“doc2”,
“file”:“http://example.com/file_doc2.doc"
},
 {
”label“:”doc3“,
”file“  :“http://example.com/file_doc3.doc"
},
 {
”label“:”doc4“,
”file“:”http://example.com/file_doc4.doc“  
} 
] 
} 
} 
  code>  pre> 
 
 

这是我的表: p>

 &lt;  td class =“tg-baqh”&gt;&lt; a class =“btn”href =“&lt;?php echo $ json ['???'];?&gt;”&gt; Doc 1&lt; / a&gt;&lt;  / td&gt; 
&lt; td class =“tg-baqh”&gt;&lt; a class =“btn”href =“  &lt;?php echo $ json ['???'];  ?&gt;“&gt; Doc 2&lt; / a&gt;&lt; / td&gt; 
&lt; td class =”tg-baqh“&gt;&lt; a class =”btn“href =”&lt;?php echo $ json ['  ???'];  ?&gt;“&gt; Doc 3&lt; / a&gt;&lt; / td&gt; 
&lt; td class =”tg-baqh“&gt;&lt; a class =”btn“href =”&lt;?php echo $ json ['  ???'];  ?&gt;“&gt;文档4&lt; / a&gt;&lt; / td&gt; 
  code>  pre> 
 
 

我需要获取下载按钮的链接。看起来像: p>

 &lt; td class =“tg-baqh”&gt;&lt; a class =“btn”href =“http://example.com/file_doc1.doc”&gt; Doc 1&lt;  / a&gt;&lt; / td&gt; 
&lt; td class =“tg-baqh”&gt;&lt; a class =“btn”href =“http://example.com/file_doc2.doc”&gt; Doc 2&lt; /  a&gt;&lt; / td&gt; 
&lt; td class =“tg-baqh”&gt;&lt; a class =“btn”href =“http://example.com/file_doc3.doc”&gt; Doc 3&lt; / a&gt  ;&lt; / td&gt; 
&lt; td class =“tg-baqh”&gt;&lt; a class =“btn”href =“http://example.com/file_doc4.doc”&gt; Doc 4&lt; / a&gt;  &lt; / td&gt; 
  code>  pre> 
  div>

You need to parse JSON data using json_decode():

$array = json_decode($json_string,true);

Now you can get data in array format. Now you need to write foreach loop for retrieve download link data.Like below:

$download_link = $array['parsel']['download_link']['Word'];
foreach($download_link as $key=>$val){
    echo '<td class="tg-baqh"><a class="btn" href="' . $val['file'] . '">'. $val['label'] .'</a></td>';
}

You can use json_decode to use your json as an array in php :

$data = json_decode($json);

Then if you know your json structure will remain the same, you can directly traverse your array to loop through the download links :

foreach($data['parsel']['download_link']['Word'] as $k => $link) {
    echo '<td class="tg-baqh"><a class="btn" href="' . $link['file'] . '">'. $link['label'] .'</a></td>';
}

You would need to use json_decode(); this would parse JSON data:

$array = json_decode($json_string,true);

Then you can write a foreach loop for the download link:

$download_link = $array['parsel']['download_link']['Word'];
foreach($download_link as $key=>$val){
    echo '<td class="tg-baqh"><a class="btn" href="' . $val['file'] . '">'. $val['label'] .'</a></td>';
}