1

Im writing a website in php that gets a JSONstring from another php-api Ive created. The string looks like this:

{
    "result": "true",
    "results": {
        "20": {
            "id": "20",
            "desc": "a b ct tr",
            "active": "1",
            "startdate": "2013-04-03",
            "starttimehour": "18",
            "starttimemin": "0",
            "enddate": "2013-04-03",
            "endtimehour": "22",
            "endtimemin": "0",
            "creator": "a"
        },
        "21": {
            "id": "21",
            "desc": "test",
            "active": "0",
            "startdate": "2013-04-04",
            "starttimehour": "18",
            "starttimemin": "0",
            "enddate": "2013-04-04",
            "endtimehour": "22",
            "endtimemin": "0",
            "creator": "a"
        }
    }
}

Ive found lots of answers on how to get information from a JSONarray but Im not using an array here. So the question is: how can I get the objects that are labeled 20, 21 and so forth(These numbers are generated by the server so I dont know which ones will be returned).

Or should I rewrite how my api returns the JSON as an array instead. Something like this:

{"result"="true", "results":[{...},{...},{...}]}
2
  • It's not clear what you need to do... PHP and JS/JSON use the term "array" to describe different things. PHP's "array" encompasses both numeric arrays (JavaScript's []) and associative arrays (JavaScript's {}). Using json_decode($input) will give you an object, using json_decode($input, true) will give you an associative array. I would recommend returning results as a numeric array regardless. Commented Apr 5, 2013 at 11:41
  • Thanks for all the answers, it helped with the json parsing. There was another problem with the other php page, it seems like its adding stuff to the string that makes the json_decode() return NULL :S Commented Apr 5, 2013 at 12:38

4 Answers 4

2
$json = json_decode($json_string, True);
foreach($json['results'] as $key => $value) {
    // access the number with $key and the associated object with $value
    echo 'Number: '.$key;
    echo 'Startdate: '.$value['startdate'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

change $json['results'] to $json->results, but your code wont work anyway. :)
You are right, i'm getting confused by mixing all those syntaxes. Edit made, added TRUE for associative arrays :-(
0

I suppose that you are getting the json by POST without any parameter, like

curl http://someapi.somedomain/someresource/ -X POST -d @data.json

so basically

$data = file_get_contents('php://input');
$object = json_decode($data);
print_r($object);

should solve your problem. and $object will be your json object that you post.

Comments

0

You do get the JSON response as a string. That's just the way JSON works. To "convert" the data to a format and structure that is easily accessible, you can use a PHP function called json_decode().

You have two choices when using the function -

  1. To convert the data into an array. json_decode($jsonString,true)
    If you use this method, you would access the data like you would for an associative array. $jsonArray['results']['21']

  2. To convert the data into an object. json_decode($jsonString)
    With this method, you would use object notation to traverse the data -
    $num = 21;
    $jsonObj->results->$num

Comments

0

First you decode the string($string) then you can loop through it and get all the properties of the objects. Remember that accessing properties is with ->prop instead of ['prop']. This way you do not have to deal with it in an array manner.

$jsoned = json_decode($string);
    foreach($jsoned->results as $o) {
        foreach($o as $key => $value) {
            echo "The key is: ".$key." and the value is: ".$value."<br>";
        }
    }

Working example what will print out:

Key is: id and value is: 20

Key is: desc and value is: a b ct tr

Key is: active and value is: 1

etc...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.