2

My problem is rather simple, at least I hope so, but I just cannot find the solution. I have a JSON string, which I got from a get request to a server.

Exemple of my JSON :

"body":[
{
   "_id":"70:ee:50:05:00:aa",
   "place":{
      "location":[
         2.4358958,
         49.503012
      ],
      "altitude":89,
      "timezone":"Europe/Paris"
   },
   "mark":8,
   "measures":{
      "02:00:00:04:f8:6a":{
         "res":{
            "1431926951":[
               7.9,
               83
            ]
         },
      "type":[
         "temperature",
         "humidity"
         ]
      },
      "70:ee:50:05:00:aa":{
         "res":{
            "1431932787":[
               1009.4
            ]
         },
         "type":[
            "pressure"
         ]
      }
  },
  "modules":[
     "02:00:00:04:f8:6a"
  ]
},

I need to accessthe temperature value, so following this example I would do this : $value = body->measures->02:00:00:04:f8:6a->res->1431926951[0].

I know i can't just do this : measures->02:00:00:04:f8:6a. The cool thing is that this difficulty can be solved doing this : $module = body->modules[0] and then I can use this variable. So it would look like this : body->measures->$module->res->1431926951[0].

What I can't seem to do though is the final step : res->1431926951[0]. This number seem completely random I can't find this value anywhere else in the JSON so I can't use the same trick I did for 02:00:00:04:f8:6a. So my question is how can I access the first value of the array located inside the object "1431926951" without knowing its name ?

Thank you very much for taking the time to help me.

EDIT : here is the code I am using in PHP

$results = json_decode($resp);
foreach($results->body as $result){
    $id = $result->_id;
    $lat = $result->place->location[0];
    $lng = $result->place->location[1];
    $module = $result->modules[0];
    $type = $result->measures->$module->type[0];
    $value = "1431926951";
    //$test = $result->measures->$module->res->$value[0];
    /*$res = $result->measures[$result->measures[0]]->res;
    $test = $res[Object.keys($res)[0]][0];*/
    echo 'Id: '.$id.' - Lat: '.$lat.' - Lng: '.$lng.' - test: '.$test."<br>";
}
1
  • why you are not using JSON.parse(json) Commented May 18, 2015 at 8:20

3 Answers 3

3

You can access with the Object.keys to the name of the property in JavaScript.

var res = obj.body.measures[ obj.body.modules[0] ].res;
console.log( res[ Object.keys(res)[0] ][0]); // prints 7.9

See demo

EDIT (PHP version, only 2 lines):

With PHP is the same but using key and stdClass (json_decode returns stdClass)

$res = $obj->body->measures->{ $obj->body->modules[0] }->res;
echo $res->{ key( (array) $res ) }[0]; // prints 7.9

See demo

Hope help.

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

2 Comments

Your solution creates an error :"Cannot use object of type stdClass as array". I am going to edit the question, I will add my code, maybe I am doing a mistake somewhere else and it will help you.
Because I didn't know that you want in PHP. I have edited my answer. See the PHP Version.
1

Here's how I did it in PHP:

<?php

$ob = '
{
"body":[
{
   "_id":"70:ee:50:05:00:aa",
   "place":{
      "location":[
         2.4358958,
         49.503012
      ],
      "altitude":89,
      "timezone":"Europe/Paris"
   },
   "mark":8,
   "measures":{
      "02:00:00:04:f8:6a":{
         "res":{
            "1431926951":[
               7.9,
               83
            ]
         },
      "type":[
         "temperature",
         "humidity"
         ]
      },
      "70:ee:50:05:00:aa":{
         "res":{
            "1431932787":[
               1009.4
            ]
         },
         "type":[
            "pressure"
         ]
      }
  },
  "modules":[
     "02:00:00:04:f8:6a"
  ]
}
]
}';

//turn the json into a PHP object
$ob = json_decode($ob);

//pre-defined array of known module names
$modules = array("02:00:00:04:f8:6a", "70:ee:50:05:00:aa");

//loop through each moduke
foreach($modules as $module){
    //get the 'res' property for that module
    $res = $ob->body[0]->measures->$module->res;
    //get a list of the object properties
    $properties = get_object_vars($res);
    //select the first property (we don't know the name) uising the current() function
    $firstProperty = current($properties);
    //and print it out
    echo "The first property of $module is: ";
    print_r($firstProperty);
    echo "<br/><br/>";
}

1 Comment

Thank you very much for taking the time to answer such a detailed solution. I tried the solution of Pedro Gamez before yours and it was working. But maybe I will need to use your solution later, thank you very much anyway
1
var jsonvar = { key: { key1: "value 1", key2: "value 2" } };
var key = Object.keys(jsonvar)[0];

This is how we can get the json key from the given json object using javascript.

Explaination: Lets say you have a json variable in javascript named jsonvar which is holding some json data. Now, to access the first key contained in parent json, then make use of array as shown above.

Object.keys(jsonvar)[0]

will return the json key: "key". We are accessing the jsonvar to access the first key which corresponds to position [0].

Hope this could solve your problem.

2 Comments

Could you elaborate your answer ? I don't understand how this would solve my problem. Maybe you could show me the implementation of your solution on my problem ? And if you know the PHP implementation it would be even better.
@PiggyGenius, i have edit/update the answer with the relevant explaination is support. I have posted the code for javascript.

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.