简体   繁体   English

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

[英]how to parse a json data and display in tabular format in php #php

this is the sample json data 这是样本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"
     }
}

Decode the json and use foreach loop. 解码json并使用foreach循环。

You need to use the json_decode for getting the array, after getting the array you need to use loop (foreach or other..) to access all the members of that array. 您需要使用json_decode来获取数组,获取数组之后,您需要使用循环(foreach或其他..)来访问该数组的所有成员。 You can directly access the members / values of that array. 您可以直接访问该数组的成员/值。

$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);

The result Object looks like this: 结果对象如下所示:

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
        )

)

Looks like your result array is a Object so you need to use -> for accessing the array items. 看起来您的结果数组是一个对象,因此您需要使用->访问数组项。

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

@RiggsFolly, Makes a clear view of this question. @RiggsFolly,使这个问题清晰可见。

Please feel free to knock me if any further help needed. 如果需要任何其他帮助,请随时与我联系。

PHP provides a handy function for converting a json string into it equivalent PHP data types called json_decode() PHP提供了一个方便的函数,用于将json字符串转换为等效的称为json_decode()的 PHP数据类型。

So using that and print_r() you can identify the contents of the json string like this 因此,使用该代码和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);

RESULTS: 结果:

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
        )

)

Now knowing the structure of the data you can pick out individual items that you want like this: 现在了解了数据的结构,您可以像这样选择所需的单个项目:

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

Or you can loop over the whole data structure with standard PHP like this, of course looking out for objects and arrays that exist inside the outer object: 或者,您可以像这样用标准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);
    }
}

The RESULT of this would be 结果将是

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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