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
Answer from JonoW on Stack OverflowScript 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))
Convert array to JSON object using JS
Convert JSON object to javascript array
How to sum multiple object with the same key in an array?
I real really Need Your Help Abou How To Send JSON Array Object POST Request Using jQuery Ajax
You can do this with .reduce:
var array = [1, 2, 3, 4];
var res = array.reduce((acc,item,index) => {
acc[`place${index}`] = item;
return acc;
}, {});
console.log(res);
var array = [1, 2, 3, 4];
const jsonObj = {}
array.forEach((v,i) => jsonObj['place'+i] = v);
console.log(jsonObj)
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