0

this is the sample json data

{"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"
     }
}
1
  • use json_decode and loop... Commented Jun 1, 2016 at 10:17

2 Answers 2

1

Decode the json and use foreach loop.

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

Please feel free to knock me if any further help needed.

Sign up to request clarification or add additional context in comments.

Comments

0

PHP provides a handy function for converting a json string into it equivalent PHP data types called json_decode()

So using that and print_r() you can identify the contents of the json string like this

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

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>

7 Comments

Sir, i already make an answer of this question similar to your post, As i know the stack users supports each other, but i see you also make another answer.
Another answer? What do you mean?
I mean you make your own answer
Yes, but it is a little more comprehensive than yours
Thats because I try to make my answers as useful as possible rather than a simple Decode the json and use foreach loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.