My JSON Value is like this -
$arr2 = array('dn'=>'NR/2014/02/1257','dd'=>'1393934346');
$arr=array('id'=>'123456','fname'=>'ABC','lname'=>'XYZ','dt'=>array($arr2));
$json = json_encode($arr));
RESULT -
{
"id":"123456","fname":"ABC","lname":"XYZ",
"dt":[
{"dn":"NR\/2014\/02\/1257","dd":1393934346}
]
}
In which dn value is NR\/2014\/02\/1257 but I want the dn value to be NR/2014/02/1257 as per my real dn value.
Can anybody help me???
You should make use of the JSON_UNESCAPED_SLASHES as a parameter to ur json_encode() function. Available since PHP 5.4.0.
<?php
$arr2 = array('dn'=>'NR/2014/02/1257','dd'=>'1393934346');
$arr=array('id'=>'123456','fname'=>'ABC','lname'=>'XYZ','dt'=>array($arr2));
echo $json = json_encode($arr,JSON_UNESCAPED_SLASHES);
OUTPUT :
{"id":"123456","fname":"ABC","lname":"XYZ","dt":[{"dn":"NR/2014/02/1257","dd":"1393934346"}]}
Demo
Turnaround for PHP versions less than 5.4.0, that doesn't support JSON_UNESCAPED_SLASHES , Doing a simple replace of backslashes does the job.
echo $json = str_replace('\\','',json_encode($arr));
You could set the JSON_UNESCAPED_SLASHES parameter when using json_encode() function (PHP version have to >= 5.4).
But the string "NR\/2014\/02\/1257" is exactly same with "NR/2014/02/1257", so the JSON_UNESCAPED_SLASHES is not necessary.
console.log("NR\/2014\/02\/1257" === "NR/2014/02/1257"); //true
But if you put the json string inside a <script> tag, which doesn't allow </ inside the strings, so it is much safer to escape the /.
$json =json_encode($arr, JSON_UNESCAPED_SLASHES);
use JSON_UNESCAPED_SLASHES
$arr2 = array('dn'=>'NR/2014/02/1257','dd'=>'1393934346');
$arr=array('id'=>'123456','fname'=>'ABC','lname'=>'XYZ','dt'=>array($arr2));
$json = json_encode($arr, JSON_UNESCAPED_SLASHES));
It is one of the predefined json constants in php http://www.php.net/manual/en/json.constants.php
var obj = jQuery.parseJSON(jsondata);
Related
this is a serialized form:
[featureimg520_624] => a:2:{s:9:"image_url";s:47:"2019/05/2019-05-27-06-07-02keerthy-suresh2.jpeg"
after unserialize I get
"image_url":"2019\/05\/2019-05-27-06-07-02keerthy-suresh2.jpeg"
with extra backslashes
I tried stripcslashes, stripslashes, str_replace
but nothing worked.
It's json_encode which is adding those backslashes. You can avoid them by using the JSON_UNESCAPED_SLASHES option to that function:
$string = 'a:1:{s:9:"image_url";s:47:"2019/05/2019-05-27-06-07-02keerthy-suresh2.jpeg";}';
$array = unserialize($string);
echo json_encode($array, JSON_UNESCAPED_SLASHES);
Output:
{"image_url":"2019/05/2019-05-27-06-07-02keerthy-suresh2.jpeg"}
I have a string like this:
$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';
How can I access items in it?
I can use regex to select them, something like /"file_id":"(.*?)"/. But that's not clean at all. Is there any approach to make a array (or an object) of string above?
It's a json string.
You need to decode it with json_decode.
The second argument (true) is to make it an array.
$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';
$arr = json_decode($str, true);
Var_dump($arr);
https://3v4l.org/9BFIC
Explode(“,{”, $str); will work for the above.
You will get array value for each file.
I currently have this code running. It is splitting the variable $json where there are },{ but it also removes these characters, but really I need the trailing and leading brackets for the json_decode function to work. I have created a work around, but was wondering if there is a more elegant solution?
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5},{"a":1,"b":2,"c":3,"d":4,"e":5}';
$individuals = preg_split('/},{/',$json);
$loop_count =1;
foreach($individuals as $object){
if($loop_count == 1){$object .='}';}
else{$object ="{".$object;}
print_r(json_decode($object));
echo '<br />';
$loop_count++;
}
?>
EDIT:
The $json variable is actually retrieved as a json object. An proper example would be
[{"id":"foo","row":1,"col":1,"height":4,"width":5},{"id":"bar","row":2,"col":3,"height":4,"width":5}]
As you (presumably) already know, the string you have to start with isn't valid json because of the comma and the two objects; it's basically two json strings with a comma between them.
You're trying to solve this by splitting them, but there's a much easier way to fix this:
The work-around is simply to turn the string into valid JSON by wrapping it in square brackets:
$json = '[' . $json . ']';
Voila. The string is now valid json, and will be parsed successfully with a single call to json_decode().
Hope that helps.
You can always add them again.
Try this:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5},{"a":1,"b":2,"c":3,"d":4,"e":5}';
$individuals = preg_split('/},{/',$json);
foreach($individuals as $key=>$val)
$individuals[$key] = '{'.$val.'}';
number as variable name not possible right? But this works
${4} = 444;
echo ${4};
Question: How much is this justified using this syntax? and where is info about this in documentation? I not found.
The syntax is covered in Variable variables. No, you are not "justified" in using this syntax. You should absolutely never do this, there is no good reason for using a number as a variable name.
Variables between brackets are considered valid (variable variables), no matter the syntax.
${'sad asda sda'} = 444;
echo ${'sad asda sda'};
// still works.
this is also works
$_4 = 444;
echo $_4; //output 444.
This is a perfectly ok json string:
$json_str = '{"1": "One", "02": "Two"}';
So if I were to decode it:
$json_object = json_decode($json_str);
the way to access the elements is:
$one = $json_object->{1};
$two = $json_object->{"02"};
I have a variable which contains a path in json_encode
/users/crazy_bash/online/test/
but json_encode converts the path to this:
\/users\/crazy_bash\/online\/test\/
Why? How can i display a normal path?
the code
$pl2 = json_encode(array(
'comment' => $nmp3,
'file' => $pmp3
));
echo($pl2);
It's perfectly legal JSON, see http://json.org/. \/ is converted to / when unserializing the string. Why worry about it if the output is unserialized by a proper JSON parser?
If you insist on having \/ in your output, you can use str_replace():
// $data contains: {"url":"http:\/\/example.com\/"}
$data = str_replace("\\/", "/", $data);
echo $data; // {"url":"http://example.com/"}
Note that it's still valid JSON by the definition of a string:
(source: json.org)
Escaped solidus is legal. But if you want a result without escaping, use JSON_UNESCAPED_SLASHESin json_encode option. However, this was added after PHP 5.4.
So, str_replace('\\/', '/', $pl2); would be helpful.
You'll have to decode it before usage.
json_decode()
That's what json_encode is supposed to do. Once you json_decode or JSON.parse it, it's fine.
var f = {"a":"\/users\/crazy_bash\/online\/test\/"}
console.log(f.a); // "/users/crazy_bash/online/test/"
var h = JSON.parse('{"a":"\/users\/crazy_bash\/online\/test\/"}');
console.log(h.a); // "/users/crazy_bash/online/test/"
I had the same problem, basically you need to decode your data and then do the encode, so it works correctly without bars, check the code.
$getData = json_decode($getTable);
$json = json_encode($getData);
header('Content-Type: application/json');
print $json;