var json_data = {"2013-01-21":1,"2013-01-22":7};
var result = [];
for(var i in json_data)
result.push([i, json_data [i]]);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows(result);
http://jsfiddle.net/MV5rj/
Answer from salexch on Stack Overflowvar json_data = {"2013-01-21":1,"2013-01-22":7};
var result = [];
for(var i in json_data)
result.push([i, json_data [i]]);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows(result);
http://jsfiddle.net/MV5rj/
If you have a well-formed JSON string, you should be able to do
var as = JSON.parse(jstring);
I do this all the time when transfering arrays through AJAX.
Convert array to JSON - javascript
Convert JSON objects to JSOn array
JSON to Array Conversion
Convert JSON object to javascript array
How to convert JSON to JSON online?
Is my data secure when using this online converter?
Is TableConvert really free to use?
Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
I made it that way:
if I have:
var jsonArg1 = new Object();
jsonArg1.name = 'calc this';
jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
jsonArg2.name = 'calc this again';
jsonArg2.value = 2.73;
var pluginArrayArg = new Array();
pluginArrayArg.push(jsonArg1);
pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:
var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
I have a system that takes an array, and turns it into a text file, using json_stringify(). I am using it to save data. Now I have a text file with the array's contents, but I can't load the data back into the array because it is a string and not an array. How can I fix this? Thanks!
What are you looking to do with the data? It looks like a better fit for a key-value pair (which is essentially all that JavaScript objects are), than for an array.
You can still access the data using array notation:
a = JSON.parse(result);
console.log(a['id']);
console.log(a['outcome']);
Hi. Please can you help with the following. My PHP returns back the following json:
{"outcome":"Case saved","id":"140"}
I basically want to convert this into an array. I’ve tried parsing it as:
JSON.parse(result)
and this returns:
Object { outcome="Case saved", id="140"}
But how do I then convert this into an array?
My original PHP code, btw, is:
$result = array(
"outcome" => "Case saved",
"id" => $new_case_id
);
$json= json_encode($result);
return $json;
Many thanks
Lee