简体   繁体   中英

Backbone and Laravel: How to convert a JSON object

From my Backbone application, Laravel is receiving the results from a POST request like this:

$input = Input::json();

This returns a JSON object, not a string. I thought I could just use json_decode to get to its properties, but json_decode expects a string. So how can I convert my JSON object into something where PHP can work with its properties?

I tried getting Input::all() from the application, but that returns an empty array.

I've tried search Google and couldn't find anything on how to do this. Thanks.

json_decode($string) must receive a json string as a parameter. By default it will return an instance of StdClass (object) with all propetires (unless you dont set second parameter = true) on success and null on failure.

Also such values as null , false , true can be returned as they are.

json_encode($anyData) can receive in fact any data and will return a JSON string.

You can use Request::getContent() to get the requested data. With it, you can pass it to json_decode() as you like.

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