admin管理员组文章数量:1023744
In the fiddle - /
<textarea id="input">
[
{
name: "Tyorry",
age: 22
}, {
name: "greg",
age: 44
}, {
name: "aff",
age: 99
}, {
name: "ben",
age: 20
}
]
var x=document.getElementById("input").value;
alert(x[0]);
There is JSON data, array of objects basically. I have 2 questions.
1) Is this JSON data in JSON.stringify format or JSON.parse format? since JSON.parse is erroring out and JSON.stringify is working properly.
2) Am getting the JSON data from textarea. but x[0] or x[3] is returning blank. basically i want to loop through the array item(which are objects) and get the values, name and age.
In the fiddle - http://jsfiddle/660m7g7k/
<textarea id="input">
[
{
name: "Tyorry",
age: 22
}, {
name: "greg",
age: 44
}, {
name: "aff",
age: 99
}, {
name: "ben",
age: 20
}
]
var x=document.getElementById("input").value;
alert(x[0]);
There is JSON data, array of objects basically. I have 2 questions.
1) Is this JSON data in JSON.stringify format or JSON.parse format? since JSON.parse is erroring out and JSON.stringify is working properly.
2) Am getting the JSON data from textarea. but x[0] or x[3] is returning blank. basically i want to loop through the array item(which are objects) and get the values, name and age.
Share Improve this question edited Feb 3, 2015 at 18:20 S1r-Lanzelot 2,2663 gold badges32 silver badges50 bronze badges asked Feb 3, 2015 at 17:59 nikhil raonikhil rao 3913 gold badges6 silver badges9 bronze badges2 Answers
Reset to default 3The value in a text area is always a string
. So if you want it as an object you'll want to use JSON.parse()
to get it. If JSON.Parse()
is failing then your JSON is in an invalid format.
To check if your JSON is valid, try using something like http://jsonlint./. The JSON provived in the fiddle is invalid.
In Plain JS use this
var x=document.getElementById("input").value;
var y = eval(x);
alert('hi '+y[0].name+ ' are you '+ y[0].age+' years old');
Plunker
In the fiddle - /
<textarea id="input">
[
{
name: "Tyorry",
age: 22
}, {
name: "greg",
age: 44
}, {
name: "aff",
age: 99
}, {
name: "ben",
age: 20
}
]
var x=document.getElementById("input").value;
alert(x[0]);
There is JSON data, array of objects basically. I have 2 questions.
1) Is this JSON data in JSON.stringify format or JSON.parse format? since JSON.parse is erroring out and JSON.stringify is working properly.
2) Am getting the JSON data from textarea. but x[0] or x[3] is returning blank. basically i want to loop through the array item(which are objects) and get the values, name and age.
In the fiddle - http://jsfiddle/660m7g7k/
<textarea id="input">
[
{
name: "Tyorry",
age: 22
}, {
name: "greg",
age: 44
}, {
name: "aff",
age: 99
}, {
name: "ben",
age: 20
}
]
var x=document.getElementById("input").value;
alert(x[0]);
There is JSON data, array of objects basically. I have 2 questions.
1) Is this JSON data in JSON.stringify format or JSON.parse format? since JSON.parse is erroring out and JSON.stringify is working properly.
2) Am getting the JSON data from textarea. but x[0] or x[3] is returning blank. basically i want to loop through the array item(which are objects) and get the values, name and age.
Share Improve this question edited Feb 3, 2015 at 18:20 S1r-Lanzelot 2,2663 gold badges32 silver badges50 bronze badges asked Feb 3, 2015 at 17:59 nikhil raonikhil rao 3913 gold badges6 silver badges9 bronze badges2 Answers
Reset to default 3The value in a text area is always a string
. So if you want it as an object you'll want to use JSON.parse()
to get it. If JSON.Parse()
is failing then your JSON is in an invalid format.
To check if your JSON is valid, try using something like http://jsonlint./. The JSON provived in the fiddle is invalid.
In Plain JS use this
var x=document.getElementById("input").value;
var y = eval(x);
alert('hi '+y[0].name+ ' are you '+ y[0].age+' years old');
Plunker
本文标签: javascriptGetting JSON data from text areaStack Overflow
版权声明:本文标题:javascript - Getting JSON data from text area - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745542447a2155243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论