I have example :
var data = [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]
I want to convert jso above to json like this result :
[{name:"eric",age:24},{name:"goulding",age:23}]
Please give me advice.
I have example :
var data = [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]
I want to convert jso above to json like this result :
[{name:"eric",age:24},{name:"goulding",age:23}]
Please give me advice.
You need to use JSON.parse with a reviver parameter:
var jsonString = '[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]';
// given a string value, returns the number representation
// if possible, else returns the original value
var reviver = function (key, value) {
var number = Number(value);
return number === number ? number : value;
};
// because the reviver parameter is provided,
// the parse process will call it for each key-value pair
// in order to determine the ultimate value in a set
var data = JSON.parse(jsonString, reviver);
When the reviver is called with reviver("name", "eric")
, it returns "eric"
because "eric"
cannot be converted to a number. However when called with reviver("age", "24")
, the number 24
is returned.
Meanwhile, as others already noted the literal [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]
is not JSON, it is an array. But the string '[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]'
represents a valid JSON formatted array object.
JSON.parse
. It's more likely that he somehow wants to "convert" strings into numbers. Bottom line, we don't know until the OP wakes up and answers the clarifying comment.
let data = [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]
This is JSON and in order to convert it to a JavaScript object (jso) we need to use parse
. If we want to manipulate JSON, we convert JSON to JSO using JSON.parse
.
let convertedToJSO = JSON.parse(data)
let data = [{name:"eric",age:24},{name:"goulding",age:23}]
And this is a JSO. If you want to print or save JSO, convert JSO to JSON using JSON.stringfy
.
let convertedToJSO = JSON.stringfy(data)