3

I'm currently using jquery and javascript to get json data, which I would like to add to a list. The raw data received from it's source looks like this;

null({"groups": "Blacksheep,Family,Voice,Technical,Band"})

with a bit of code my input comes out as

Blacksheep,Family,Voice,Technical,Band 

My code takes these values and puts them though a for loop, looping once for each entry. it then takes that entry's name and puts it into a <li> field on my html document inside of a <ul> tag named people.

for (i=0 ; i < input.length ; i++){
    console.log(input);
    document.getElementById("people").innerHTML += "<li>" + input[i] + "</li>";
}

I tried to turn it into an array but adding a '[' on each side, but obviously it isn't so easy. at the moment it takes the json entry and lists each letter instead of word.

is there a way to separate each word from json? or a way to interpret it as an array?

1
  • Did you try this: {"groups": ["Blacksheep","Family","Voice","Technical","Band"]}
    – Ashkan
    Aug 30, 2012 at 13:35

4 Answers 4

7

you can split the var

"Blacksheep,Family,Voice,Technical,Band".split(',');

this returning a string of array..

0
3

The data is a string. you can use .split(delimiter) to split the words.

//split by ",". now input is an array
input = input.split(',');
2

Take the string and use .split to convert it to an Array

2

This should do it. As all the answer mention split the string.

var arr = "Blacksheep,Family,Voice,Technical,Band";
var input = arr.split(",");

for (i=0 ; i < input.length ; i++){
    console.log(input);
    document.getElementById("people").innerHTML += "<li>" + input[i] + "</li>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.