简体   繁体   中英

Process SQL return into JSON

I'm playing with building my first JSON API here. I have the following function that returns JSON from my SQL query:

function processResponse($response)
{
      // if (!$key) echo '[';
      for ($i=0;$i<count($response);$i++) {
        echo ($i>0?',':'').json_encode($response);
      }
      // if (!$key) echo ']';
}

Notice I have commented the opening and closing [ ] brackets as I was noticing multiple (unnecessary) nested arrays. This allows something like this: $json[0]->email; otherwise it would be a nested array and I would need to do something like $json[0][0]->email;

However, when I do a json_decode() on my processed output I get a null. Adding a [] back resolves this.

So I guess my question is why does json_decode() return null for this:

[{"inventory_id":7,"start":null,"expiration":null,"created":"2016-12-05 10:58:23","updated":"2016-12-08 15:29:56","item_name":"Tour Ticket (Per Person)","default_rate":null,"quantity":5,"bookings_count":"6","itinerary_count":"8","group_name":"Winery","location_name":"Location 1","status":"Active","type_name":"Tour","username":"email@address.com","user_id":4},{"inventory_id":20,"start":null,"expiration":null,"created":"2016-12-06 12:33:18","updated":"2016-12-08 15:41:12","item_name":"Tasting Ticket","default_rate":null,"quantity":10,"bookings_count":"0","itinerary_count":"0","group_name":"Winery","location_name":"Location 1","status":"Archived","type_name":"Ticket","username":"email@address.com","user_id":4}]

and why does this work:

[[{"inventory_id":7,"start":null,"expiration":null,"created":"2016-12-05 10:58:23","updated":"2016-12-08 15:29:56","item_name":"Tour Ticket (Per Person)","default_rate":null,"quantity":5,"bookings_count":"6","itinerary_count":"8","group_name":"Winery","location_name":"Location 1","status":"Active","type_name":"Tour","username":"email@address.com","user_id":4},{"inventory_id":20,"start":null,"expiration":null,"created":"2016-12-06 12:33:18","updated":"2016-12-08 15:41:12","item_name":"Tasting Ticket","default_rate":null,"quantity":10,"bookings_count":"0","itinerary_count":"0","group_name":"Winery","location_name":"Location 1","status":"Archived","type_name":"Ticket","username":"email@address.com","user_id":4}]]

Is there a better way for me to construct the JSON output?

You only need return your data from db as an associative array then you call json_encode and you will be happy to go. it does all the work for you.

$data = ["name" => "john doe", "age" : 33];
print json_enconde($data); // {"name" : "john doe", "age": 33 }

$data = [
    ["name" => "john doe", "age" : 33],
    ["name" => "mary doe", "age" : 28]
];
print json_enconde($data); // [{"name" : "john doe", "age": 33 }, {"name" : "mary doe", "age": 28 }]

As you can see... $data only need to be an associative array, so fetch as it from your db

Be aware that sometimes your data don't fit UTF8 to be parsed to json. this is specially true when you are retuning blobs from your db.

I really recomend you to read this

http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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