I have an object:
[{"Spalte":3}]
How can I get only 3 ?
I tried something like that:
var s = JSON.stringify(data); // [{"Spalte":3}] as String
var d = parseInt(s); // typeof d = number,
When I tried to alert(d) I receive NaN.
You probably meant to do
var d = JSON.parse(s)[0].Spalte;
s contains a serialization of an object as JSON. So we parse it with JSON.parse to get back the original object and then use standard javascript to extract the numeric field. Notice there is no need for parseInt.
Alternatively, you can stringify the numeric field by itself:
var data = [{"Spalte":3}];
var s = JSON.stringify(data[0].Spalte);
var d = JSON.parse(s);
or even:
var data = [{"Spalte":3}];
var s = data[0].Spalte + ""; // simply convert a number to string
var d = parseInt(s); // parse the string back to a number.
data[0].Spalte does the trick. Also, you probably meant JSON.parse(s) instead of JSON.parse('3')?var data = [{
"Spalte": 3,
valueOf() {
return Spalte;
}
}];
console.log(+data[0]) // 3
data[0].Spalte?