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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

The 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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

The 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