How to Use JSON.Stringify and JSON_Decode() Properly

how to use JSON.stringify and json_decode() properly

You'll need to check the contents of $_POST["JSONfullInfoArray"]. If something doesn't parse json_decode will just return null. This isn't very helpful so when null is returned you should check json_last_error() to get more info on what went wrong.

How to get JSON.stringify into PHP with json_decode without AJAX?

Probably the magic quotes screw up your JSON string and PHP doesn't recognize it anymore. Use stripslashes() before you hand it over to json_decode():

$a = json_decode(stripslashes($_POST['json']));
var_dump($a);

How to json_decode an json array received from JavaScript?

I am tranfering a note from What does FILTER_SANITIZE_STRING do? but the entire accepted answer in that question explains it a lot better:

First - php_filter_strip. It doesn't do much, just takes the flags you pass to the function and processes them accordingly. It does the well-documented stuff.

Then we construct some kind of map and call php_filter_encode_html. It's more interesting: it converts stuff like ", ', & and chars with their ASCII codes lower than 32 and higher than 127 to HTML entities, so & in your string becomes &. Again, it uses flags for this.

Then we get call to php_strip_tags_ex, which just strips HTML, XML and PHP tags (according to its definition in /ext/standard/string.c) and removes NULL bytes, like the comment says.

(Emphasised the important part).

In short FILTER_SANITIZE_STRING will break your JSON because it will encode things that it should not. If you want to validate this input do not use this filter.

The answer here is to not use FILTER_SANITIZE_STRING.
The sensible way to validate a JSON string is to do json_decode and check if it's null.

$jsonStr = filter_input(\INPUT_GET, 'myparam'); 
var_dump($jsonStr);
var_dump(json_decode($jsonStr, true));

Unable to json_decode an object I send with JSON.stringify()

In-case someone cares about the solution:

I had to us encodeURIcomponenet() on the stringified object.

How to convert Javascript JSON.stringify() to PHP Array

you can't pass js data to php ,because php renderd first.

but
you can call ajax and return blade to response

your ajax call

const loadTemplate = (locationinfo) => {
$.ajax({
data: {
'locationinfo': locationinfo,
},
type: "post",
url: "{{route('someRoute')}}",
success: function (data) {
let info = `
<div class="location-info">
<h1>${locationinfo.business_name}</h1>
${data}
</div>`;
return info;
},
error: function () {
//has error
}
});
}

your route

Route::get('/getAjaxData', [AjaxController::class,'show']) ->name('someRoute'); // but i use __invoke controller

your controller

<?php

namespace YOUR_NAMESPACE;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AjaxController extends Controller
{
public function show(Request $request)
{
$data = $request->input('locationinfo');

return view('pages/business-space/templates/t1')->with([
'locationinfo' => $data,
]);
}

}

NodeJS JSON.stringify sending argument to PHP json_decode - return null

First, you haven't defined $argv[1].
Second, it's not JSON in the eyes of PHP, it's a string, so it needs to be JSON Encoded, before PHP reads it.

Command line:

php test.php "{ \"access_token\": \"acccess_token\", \"id_token\": \"id_token\", \"refresh_token\": \"refresh-token\", \"token_type\": \"token_type\", \"expiry_date\": 1515578624982, \"expires_in\": 3600 }"

PHP script:

$json = json_decode($_SERVER['argv']['1'], true);

if (json_last_error()) { var_dump($_SERVER['argv']['1']); die(); }

echo "CHECK-ERROR".json_last_error().PHP_EOL."-----".PHP_EOL;

echo "END RESULTS".PHP_EOL.json_encode($json).PHP_EOL."-------".PHP_EOL;

Should do the trick.

Can't do json_decode when saved data by JSON.stringify

You have posted json string in result field so you will not need to json_code before saving data in database

public function setDataAttribute($value) {
$this->attributes['data'] = $value;
}

it would be work fine..

This is json encode data :

 '[{"title":"a","content":"a2"},{"title":"b","content":"b2"}]'

And again encode json string

json_encode('[{"title":"a","content":"a2"},{"title":"b","content":"b2"}]');

Then result

"[{\"title\":\"a\",\"content\":\"a2\"},{\"title\":\"b\",\"content\":\"b2\"}]"


Related Topics



Leave a reply



Submit