简体   繁体   中英

How to get array from JSON?

I am trying to fetch data from MySQL table that have 2 columns, Temperature and Value. I want to store these values to JSON and then to pass to the client side script. My PHP code is: database2json.php:

<?php
    $con = mysql_connect("localhost", "root", "123456");
    if (!$con) {
        die('Could not connect:' . mysql_error());
    }
    mysql_select_db("klima", $con);
    $result = mysql_query("select Dan, Temperatura from TEMPERATURA");
        $niz = array();
    while ($row = mysql_fetch_array($result)) {
        $niz[$row['Dan']] = $row['Temperatura'];
    }
        mysql_close($con);
        $obj = json_encode($niz);
        echo $obj;
?>

When I run this file on server I get this:

{"1":"-1","2":"0","3":"0","4":"0","5":"4","6":"5","7":"3","8":"2","9":"2","10":"1","11":"-2","12":"-2","13":"0","14":"1","15":"-2","16":"-1","17":"-1","18":"-2","19":"-1","20":"3","21":"-1","22":"0","23":"1","24":"3","25":"1","26":"1","27":"-1","28":"-1","29":"4","30":"5","31":"5"}

That is what is expected.

Html is nothing special.

index.html:

<html>
    <head>
        <title>jQuery</title>
        <script src="jquery.js" type="text/javascript"></script>
        <script src="custom.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="id1"></div>
    </body>
</html>

Now I call php from jQuery and to show these values.

custom.js:

$(document).ready(function(){
    $.post('database2json.php', function(data){
        $('#id1').html(data);
    },
    "json");
});

This also gives same output like php:

{"1":"-1","2":"0","3":"0","4":"0","5":"4","6":"5","7":"3","8":"2","9":"2","10":"1","11":"-2","12":"-2","13":"0","14":"1","15":"-2","16":"-1","17":"-1","18":"-2","19":"-1","20":"3","21":"-1","22":"0","23":"1","24":"3","25":"1","26":"1","27":"-1","28":"-1","29":"4","30":"5","31":"5"}

Now I dont know how to convert this into array of [Dan, Temperatura]. I need this array to forward to chart and plot data (I am not asking about plotting, just to get array).

How to achieve this?

Your output

{"1":"-1","2":"0","3":"0",...,"31":"5"}

Is a JavaScript object in its current form. You can simply access it as:

alert(data["1"]);
// -1

alert(data["31"]);
// 5

Note that the common syntax for object literals is dot notation: object.propertyname , but that will not work for numeric property names like your 1-31 indexes. So instead you use the bracketed property name as in data["1"] .

If you really need it as an indexed array, you can convert it as:

var array = [];
for (key in data) {
  array[key] = data[key];
}
// Now array is an Array with similar structure to the object data

Update

There is another possibility to get this data as a proper array directly from PHP. You can wrap the output in an additional array like this:

// Wrap the array in another array indexed as niz
$obj = json_encode(array("niz" => $niz));
echo $obj;

I have something like your code in my project. It is like this:

$final = array('msg' => $msg, 'result' => $result);
echo json_encode($final);

As you see, I made an array with 2 key and value. This code works fine for me. try to obey the method above to create your array and test it again. I hope you can solve your problem.

If I understand correctly, you want to convert PHP's JSON output to an actual array, right?

You can simply use eval() for that.

myArray = eval(data);

WARNING : Eval is considered unsafe and you should only use it for trusted sources. Make sure that there is absolutely no way for anything else than your PHP script to be evaluated.

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