-1

I string contain \ character. All character inserted properly but this '\'.

My json created is like this : [{"desc":"\"}] PHP Code :

$mparajson = '{"arrayofvalues":false,"recordcount":1,"rows":[{"desc":"\\"}]}';

$jsall  = array();
$jsone  = array();
$jsall_all = json_decode($mparajson, true);
$jsall =  $jsall_all["rows"];

$x = 0;
$jsone=$jsall[$x];

$jsone is null

Silambarasan R
  • 1,346
  • 15
  • 22
Honey Last
  • 255
  • 1
  • 3
  • 15

2 Answers2

0

Make it like below (Four backslashes).

$mparajson = '{"arrayofvalues":false,"recordcount":1,"rows":[{"desc":"\\\\"}]}';

You can test this as follows,

$sample_array = ['arrayofvalues' => false, 'recordcount' => 1, 'rows' => ['desc' => '\\']];

echo '<pre>';
print_r(json_decode(json_encode($sample_array), true));
exit;

Ref: How to deal with backslashes in json strings php

Silambarasan R
  • 1,346
  • 15
  • 22
  • Why the redundant json_decode? Do you mean print_r(json_decode($sample_array, TRUE)); https://snipboard.io/A2v4QJ.jpg – mrwpress Dec 06 '19 at 06:19
  • See the reference link for more detailed information about `json_encode()` and `json_decode()`. – Silambarasan R Dec 06 '19 at 06:24
  • Ahhh, hard to see with this formatting. Just an odd way of writing it out I guess. – mrwpress Dec 06 '19 at 06:41
  • `$mparajson = '{"arrayofvalues":false,"recordcount":1,"rows":[{"desc":"\\"}]}';` How did you get this line of code. Provide the details about before this line of code. – Silambarasan R Dec 06 '19 at 06:43
0

Your JSON string is not formatted right. Try this:

$mparajson = '{"arrayofvalues":false,"recordcount": 1, "rows":{"desc":"something with slashes \\\\"}}';

When escaping in JSON you have to use a backslash. Since you had two back slashes, you need two out in front.

mrwpress
  • 311
  • 1
  • 8