I have json response and wants to get some data from it. Here's my json response code:
{
"links": [
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"userId": 1,
"id": 3,
"title": "fugiat veniam minus",
"completed": false
},
{
"userId": 1,
"id": 4,
"title": "et porro tempora",
"completed": true
},
{
"userId": 1,
"id": 5,
"title": "laboriosam mollitia et enim quasi adipisci quia provident illum",
"completed": false
}
]
}
Please help me to get all values of title and append them to html span tag.
CodePudding user response:
You can pass the response to JSON.parse() function and then iterate over the "links" object
var data = JSON.parse(response);
for(var i=0;i<data.links.length;i ){
console.log(data.links[i]);
}
CodePudding user response:
This is very similar to Deaguve answer. I just added JSON.stringify(json) to JSON.parse and console logged titles.
let parsed = JSON.parse(JSON.stringify(json));
for(let i = 0; i < parsed.links.length; i ) {
console.log(parsed.links[i].title);
}