简体   繁体   中英

Json decode in javascript

i have encoded my required data in the json object ,but i want to decode the json object into a javscript array, my json encoded object is :

{"product_id":"62","product_quantity":"65"}

however i want to use this json in my java script and want it to be available to a java script array

so if i do :

var arr = new Array()
arr = <?php json_decode('$json_object',TRUE); ?>;

however when i check my page source i get null ie arr = how can i assign my json object converted to array to java script array ? further how to access the json objects from java script array ?

json_decode returns a PHP data structure. If you want to serialise that to a JavaScript data structure you have to pass it through json_encode (and then actually echo the string that it returns).

Note that json_encode outputs a JavaScript data structure that is safe for injecting into a <script> element in an HTML document. Not all JSON is safe to do that with (PHP adds additional escape sequences, and will transform plain strings, numbers, null values, etc (which aren't legal JSON on their own).

Note that there is also no point in creating a new array and assigning it to arr if you are going to immediately assign something else to arr .

Also note that '$json_object' will give you a string starting with the $ character and then the name of the variable. Single quoted string in PHP are not interpolated.

var arr;
arr = <?php echo json_encode(json_decode($json_object,TRUE)); ?>;

Also note that this JSON:

{"product_id":"62","product_quantity":"65"}

Will transform in to a PHP associative array or a JavaScript object (which is not an array).

So given this PHP:

<?php
    $json_object = '{"product_id":"62","product_quantity":"65"}';
?>
<script>
    var arr;
    arr = <?php echo json_encode(json_decode($json_object,TRUE)); ?>;
    alert(arr.product_id);
</script>

You get this output:

<script>
    var arr;
    arr = {"product_id":"62","product_quantity":"65"};
    alert(arr.product_id);
</script>

Which alerts 62 when run.

You could push the JSON objects into javascript array and iterate through the array, selecting the appropriate fields you need. Fixed it..

    var json = {"product_id":"62","product_quantity":"65"}; 
    var array = new Array();
    array.push(json);   
    for(var i = 0; i < array.length; i++){
       console.log(array[i].product_id)
    }

Okay so to start off :

the json string generated in PHP can be used in Javascript as an Object. If you declare the variable as an array to start with then it might conflict.

anyway this should work :

<?php 
$error_fields_structure = array(
     'product_id' => 4531
    ,'main_product_quantity' => 2
);

$json_object = json_encode($error_fields_structure);
?>

<html>
    <head>
        <script>
            var jsonstring = <?php echo (isset($json_object) ? $json_object : 'nothing here'); ?>

            for( var i in jsonstring ){
                alert( i +' == ' +jsonstring[i] );
            }
        </script>
    </head>

    <body>

    </body>
</html>

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