如何解析json数据并在php #php中以表格格式显示

时间:2016-06-01 10:16:29

标签: php json api

这是示例json数据

{"user":{
         "age":22,
         "avatar":"https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif",
         "avatar150":"https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif",
         "averageDailySteps":0,
         "corporate":false,
         "dateOfBirth":"1993-08-03",
         "displayName":"nnwin",
         "distanceUnit":"METRIC",
         "encodedId":"4N9GR6",
         "features":{"exerciseGoal":true},
         "foodsLocale":"en_GB",
         "fullName":"nnwin",
         "gender":"MALE",
         "glucoseUnit":"METRIC",
         "height":176,
         "heightUnit":"METRIC",
         "locale":"en_GB",
         "memberSince":"2016-05-25",
         "nickname":"nveeen",
         "offsetFromUTCMillis":19800000,
         "startDayOfWeek":"MONDAY",
         "strideLengthRunning":91.60000000000001,
         "strideLengthRunningType":"default",
         "strideLengthWalking":73,
         "strideLengthWalkingType":"default",
         "timezone":"Asia/Kolkata","topBadges":[],
         "waterUnit":"METRIC",
         "waterUnitName":"ml",
         "weight":104,
         "weightUnit":"METRIC"
     }
}

2 个答案:

答案 0 :(得分:1)

  

解码json并使用foreach循环。

您需要使用json_decode来获取数组,在获取数组后,您需要使用循环(foreach或其他..)来访问该数组的所有成员。您可以直接访问该数组的成员/值。

$json = '{"user":{"age":22,"avatar":"https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif","avatar150":"https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif","averageDailySteps":0,"corporate":false,"dateOfBirth":"1993-08-03","displayName":"nnwin","distanceUnit":"METRIC","encodedId":"4N9GR6","features":{"exerciseGoal":true},"foodsLocale":"en_GB","fullName":"nnwin","gender":"MALE","glucoseUnit":"METRIC","height":176,"heightUnit":"METRIC","locale":"en_GB","memberSince":"2016-05-25","nickname":"nveeen","offsetFromUTCMillis":19800000,"startDayOfWeek":"MONDAY","strideLengthRunning":91.60000000000001,"strideLengthRunningType":"default","strideLengthWalking":73,"strideLengthWalkingType":"default","timezone":"Asia/Kolkata","topBadges":[],"waterUnit":"METRIC","waterUnitName":"ml","weight":104,"weightUnit":"METRIC"}}';
$result = json_decode ($json);

结果对象如下所示:

stdClass Object
(
    [user] => stdClass Object
        (
            [age] => 22
            [avatar] => https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif
            [avatar150] => https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif
            [averageDailySteps] => 0
            [corporate] => 
            [dateOfBirth] => 1993-08-03
            [displayName] => nnwin
            [distanceUnit] => METRIC
            [encodedId] => 4N9GR6
            [features] => stdClass Object
                (
                    [exerciseGoal] => 1
                )

            [foodsLocale] => en_GB
            [fullName] => nnwin
            [gender] => MALE
            [glucoseUnit] => METRIC
            [height] => 176
            [heightUnit] => METRIC
            [locale] => en_GB
            [memberSince] => 2016-05-25
            [nickname] => nveeen
            [offsetFromUTCMillis] => 19800000
            [startDayOfWeek] => MONDAY
            [strideLengthRunning] => 91.6
            [strideLengthRunningType] => default
            [strideLengthWalking] => 73
            [strideLengthWalkingType] => default
            [timezone] => Asia/Kolkata
            [topBadges] => Array
                (
                )

            [waterUnit] => METRIC
            [waterUnitName] => ml
            [weight] => 104
            [weightUnit] => METRIC
        )

)

您的结果数组看起来像是一个Object,因此您需要使用->来访问数组项。

foreach($result as $val){
    //what ever you want 
}

@RiggsFolly,清楚地了解这个问题。

如果需要进一步的帮助,请随时敲我。

答案 1 :(得分:0)

PHP提供了一个方便的函数,用于将json字符串转换为等效的PHP数据类型json_decode()

因此使用它和print_r()可以像这样识别json字符串的内容

<?php
$str = '{"user":{
         "age":22,
         "avatar":"https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif",
         "avatar150":"https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif",
         "averageDailySteps":0,
         "corporate":false,
         "dateOfBirth":"1993-08-03",
         "displayName":"nnwin",
         "distanceUnit":"METRIC",
         "encodedId":"4N9GR6",
         "features":{"exerciseGoal":true},
         "foodsLocale":"en_GB",
         "fullName":"nnwin",
         "gender":"MALE",
         "glucoseUnit":"METRIC",
         "height":176,
         "heightUnit":"METRIC",
         "locale":"en_GB",
         "memberSince":"2016-05-25",
         "nickname":"nveeen",
         "offsetFromUTCMillis":19800000,
         "startDayOfWeek":"MONDAY",
         "strideLengthRunning":91.60000000000001,
         "strideLengthRunningType":"default",
         "strideLengthWalking":73,
         "strideLengthWalkingType":"default",
         "timezone":"Asia/Kolkata","topBadges":[],
         "waterUnit":"METRIC",
         "waterUnitName":"ml",
         "weight":104,
         "weightUnit":"METRIC"
     }
}';

$obj = json_decode($str);

print_r($obj);

结果:

stdClass Object
(
    [user] => stdClass Object
        (
            [age] => 22
            [avatar] => https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif
            [avatar150] => https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif
            [averageDailySteps] => 0
            [corporate] =>
            [dateOfBirth] => 1993-08-03
            [displayName] => nnwin
            [distanceUnit] => METRIC
            [encodedId] => 4N9GR6
            [features] => stdClass Object
                (
                    [exerciseGoal] => 1
                )

            [foodsLocale] => en_GB
            [fullName] => nnwin
            [gender] => MALE
            [glucoseUnit] => METRIC
            [height] => 176
            [heightUnit] => METRIC
            [locale] => en_GB
            [memberSince] => 2016-05-25
            [nickname] => nveeen
            [offsetFromUTCMillis] => 19800000
            [startDayOfWeek] => MONDAY
            [strideLengthRunning] => 91.6
            [strideLengthRunningType] => default
            [strideLengthWalking] => 73
            [strideLengthWalkingType] => default
            [timezone] => Asia/Kolkata
            [topBadges] => Array
                (
                )

            [waterUnit] => METRIC
            [waterUnitName] => ml
            [weight] => 104
            [weightUnit] => METRIC
        )

)

现在知道数据的结构,你可以选择你想要的单个项目:

<?php
echo $obj->user->age;           // 22
echo $obj->user->dateOfBirth    // 1993-08-03

或者您可以像这样使用标准PHP循环遍历整个数据结构,当然要注意外部对象中存在的对象和数组:

foreach ( $obj->user as $name => $value ) {
    if ( $name == 'features' ) {
        foreach ( $obj->user->features as $n => $v ) {
            echo sprintf("<td>%s</td><td>%s</td>\n", $n, $v);
        }
    } elseif ( $name == 'topBadges') {
        foreach ( $obj->user->features as $n => $v ) {
            echo sprintf("<td>%s</td><td>%s</td>", $n, $v);
        }
    } else {
        echo sprintf("<td>%s</td><td>%s</td>\n", $name, $value);
    }
}

结果将是

<td>age</td><td>22</td>
<td>avatar</td><td>https://static0.fitbit.com/images/profile/defaultProfile_100_male.gif</td>
<td>avatar150</td><td>https://static0.fitbit.com/images/profile/defaultProfile_150_male.gif</td>
<td>averageDailySteps</td><td>0</td>
<td>corporate</td><td></td>
<td>dateOfBirth</td><td>1993-08-03</td>
<td>displayName</td><td>nnwin</td>
<td>distanceUnit</td><td>METRIC</td>
<td>encodedId</td><td>4N9GR6</td>
<td>exerciseGoal</td><td>1</td>
<td>foodsLocale</td><td>en_GB</td>
<td>fullName</td><td>nnwin</td>
<td>gender</td><td>MALE</td>
<td>glucoseUnit</td><td>METRIC</td>
<td>height</td><td>176</td>
<td>heightUnit</td><td>METRIC</td>
<td>locale</td><td>en_GB</td>
<td>memberSince</td><td>2016-05-25</td>
<td>nickname</td><td>nveeen</td>
<td>offsetFromUTCMillis</td><td>19800000</td>
<td>startDayOfWeek</td><td>MONDAY</td>
<td>strideLengthRunning</td><td>91.6</td>
<td>strideLengthRunningType</td><td>default</td>
<td>strideLengthWalking</td><td>73</td>
<td>strideLengthWalkingType</td><td>default</td>
<td>timezone</td><td>Asia/Kolkata</td>
<td>exerciseGoal</td><td>1</td>
<td>waterUnit</td><td>METRIC</td>
<td>waterUnitName</td><td>ml</td>
<td>weight</td><td>104</td>
<td>weightUnit</td><td>METRIC</td>