如何打印这个json字符串列表?

时间:2016-06-03 14:06:12

标签: php json

我在数据库表中构造了一个以下json获取值。  现在我想在json下打印到php中的无序列表。  我怎样才能在json下面打印这个列表。

这是我的数组和预期输出。

{
    "id": "6",
    "name": "aaaaa",
    "actove_from": "2000",
    "firstlevelrelations": {
        "1": {
            "name": "bbbbb",
            "active_from": "1990"

        },
        "5": {
            "name": "cccccc",
            "active_from": "2003"

        },
        "4": {
            "name": "dddddd",
            "active_from": "1992",

            "2": {
                "name": "fffff",
                "active_from": "1990"

                            }

        },
        "7": {
            "name": "ggggg",
            "active_from": "2015"

        },
        "9": {
            "name": "hhhhhh",
            "active_from": "1990",

            "10": {

                "name": "yyyyy",
                "active_from": "1997"

            }
        }

    }
}

ouptut:

<ul>
    <li>aaaaaa</li>
     <li>2000</li>
     <ul>
         <li>bbbbbb</li>
         <li>1990</li>
         <li>ccccc</li>
         <li>2003</li>
         <li>dddddd</li>
         <li>1992
            <ul>
                <li>fffff</li>
               <li>1990</li>             
            </ul>
         </li>
         <li>gggggg</li>
         <li>2015</li>
         <li>hhhhhh</li>
         <li>1990
            <ul>
                <li>yyyyyy</li>
               <li>1997</li>             
            </ul>
         </li>
     </ul>
</ul>   

3 个答案:

答案 0 :(得分:2)

// decode the string as assoc array
$json = json_decode($your_json_string_here, true);

function menu( $arr ) {
    $result = "";
    foreach( $arr as $key => $item ) {
        // if the item is array calls the menu function
        $result .= "<li>" . (is_array($item) ? menu($item) : $item)."</li>";
    }
    return "<ul>" . $result . "</ul>";
}

echo menu($json);

这将创建所需的输出(但没有缩进)

答案 1 :(得分:0)

我没有为你解决所有问题,但这应该可以让你到达目的地。

$array = json_decode($json, true);
foreach($array as $key => $value)
{
    if(is_array($value))
    {
        //start building your sublist
    }else{
        //start building your list
    }

}

答案 2 :(得分:-1)

$jsonvar = json_decode('{"id":"6","name":"aaaaa","actove_from":"2000","firstlevelrelations":{"1":{"name":"bbbbb","active_from":"1990"},"5":{"name":"cccccc","active_from":"2003"},"4":{"name":"dddddd","active_from":"1992","2":{"name":"fffff","active_from":"1990"}},"7":{"name":"ggggg","active_from":"2015"},"9":{"name":"hhhhhh","active_from":"1990","10":{"name":"yyyyy","active_from":"1997"}}}}', true); // now you can access like an array.

$id = $jsonvar['id'];
// so on and so forth

//to access the firstlevelrelation
for($i=0;$i<$jsonvar['firstlevelrelation'];$i++){
    echo $jsonvar['firstlevelrelation'][$i]; // here you will be accessing each element inside the firstLevelRelation field.
    // so for example, you wanna acess the name from each element.
    $nameInsideFirstLevelRelation = $jsonvar['firstlevelrelation'][$i]['name'];

}

或者,如果索引可以更改名称

foreach($jsonvar as $key=>$value){
    //if it is just a simple value
    $valuesInsideYourJson = $value;

    //if it is an array of other objects.
    if(is_array($valuesInsideYourJson)){
        foreach($valuesInsideYourJson as $val){
            // this in inside the firstlevelrelation
            // do whatever you want
        }
    }
}

您可以在此处访问原始数据,现在可以在DOM元素标记之间进行打印。