-1

i work on project, where there are server that send to my php application json data and then i put it in database. I receive a POST request from the server and i can get json with this : receive_json.php:

$payload = file_get_contents('php://input');

and there is the form of the JSON:

{
"applicationID": "123",
"applicationName": "temperature-sensor",
"deviceName": "garden-sensor",
"devEUI": "0202020202020202",
"rxInfo": [
    {
        "gatewayID": "0303030303030303",          // ID of the receiving gateway
        "name": "rooftop-gateway",                 // name of the receiving gateway
        "time": "2016-11-25T16:24:37.295915988Z",  // time when the package was received (GPS time of gateway, only set when available)
        "rssi": -57,                               // signal strength (dBm)
        "loRaSNR": 10,                             // signal to noise ratio
        "location": {
            "latitude": 52.3740364,  // latitude of the receiving gateway
            "longitude": 4.9144401,  // longitude of the receiving gateway
            "altitude": 10.5,        // altitude of the receiving gateway
        }
    }
],
"txInfo": {
    "frequency": 868100000,  // frequency used for transmission
    "dr": 5                  // data-rate used for transmission
},
"adr": false,                  // device ADR status
"fCnt": 10,                    // frame-counter
"fPort": 5,                    // FPort
"data": "...",                 // base64 encoded payload (decrypted)
"object": {                    // decoded object (when application coded has been configured)
    "temperatureSensor": {"1": 25},
    "humiditySensor": {"1": 32}
 }
}

i'm new user of JSON, so i put directly the text JSON to my database like this :

 $payload = file_get_contents('php://input');

 $req = $bdd->prepare( '                             
                           INSERT INTO nods(payload)                               
                           VALUES (:payload);
                      ' );

$req->execute(array('payload' => $payload));

the result : enter image description here I would like to browse " $payload " and put the information in php variables like:

$applicationID = .... ; $rxInfo[] = .... ;

i already try somthing like "foreach" but i could not succes, can you help me please, thanks :).

Best regard,

DIDOUH JAWAD

1 Answers1

-1

You can use json_decode to get a PHP array representation of the JSON

$array = json_decode($payload, true);

var_dump($array);

http://php.net/manual/en/function.json-decode.php

beingalex
  • 2,416
  • 4
  • 32
  • 71
  • 2
    this has been answered so many times it is not worth an answer, but a close vote. – YvesLeBorg Sep 21 '18 at 14:13
  • I don't believe that just because it's been answered, it warrants a down-vote. It is a valid answer. – beingalex Sep 21 '18 at 14:13
  • You're right it doesn't, I upvoted you as you shouldn't be penalised for trying to participate in the community and helping even if the answer does already exist – TommyBs Sep 21 '18 at 14:15
  • whene i use var_dump($array); the result is "C:\wamp64\www\json_anal.php:37:null" the ligne "37" is var_dump($array); ... – jawad didouh Sep 21 '18 at 14:27
  • i think that the the encoded data is deeper than the recursion limit this is why i have "null like result" what is solution ?? hmmm – jawad didouh Sep 21 '18 at 14:31
  • 1
    @TommyBs People are free to vote however they please. Additionally the answer fails to see that comments are not supported in JSON. – MonkeyZeus Sep 21 '18 at 14:57
  • I think that highlights why people should leave a comment when downvoting then. I’m all for voting when you please, but with the new code of conduct I think we should try not to penalise those actively trying to help others without some kind of constructive criticism. – TommyBs Sep 21 '18 at 15:00
  • @TommyBs At face value of the original question at hand "browse a JSON and fill PHP variables", the first comment satisfies someone may have downvoted. If OP's question was "My JSON is not decoding, why?" then that would warrant more thought and would be closed as a dupe to either https://stackoverflow.com/q/2348152/2191572 or https://stackoverflow.com/q/244777/2191572 – MonkeyZeus Sep 21 '18 at 15:09
  • Fair enough I’m not trying to get drawn into any kind of argument, and I must admit I’m guilty of glancing at the question and ignoring the “comments” section of it, but again that’s where a comment here to help the answering user either remove or fix their answer would be useful and overall benefit the site and quality of responses – TommyBs Sep 21 '18 at 15:12
  • 2
    @TommyBs I'm not arguing, just trying to guide. If you feel strongly about something then I suggest starting a discussion on https://meta.stackoverflow.com/. Overall, I would guess that you ended up on this site because of to the various high-quality content which exists here. If you're suggesting that repeating the same Q&A thousands of times is beneficial to the self-preservation of what makes this site great then let's agree to disagree. – MonkeyZeus Sep 21 '18 at 15:21
  • thanks guy i found the major error it was "the comments" , but i always can't brows recursion array because there are an array in the array !!!! a don't now how i should be do – jawad didouh Sep 21 '18 at 15:41
  • if you see my JSON DATA, i can acces to applicationID by using "echo($var['applicationName']);" so the result are => "test_nod" !!!!!!!! but if i want the data "name" who are in "rxinfo" how i'm suppose to do ? please help. Remarque i add this after the payload => $var = json_decode($payload,true); – jawad didouh Sep 21 '18 at 15:53