将JSON数组数据排列成html元素

时间:2016-02-03 06:39:09

标签: jquery html

我有这样的JSON数据:

{"_id":"56af2ca2eb91bd4721443037","username":"xxxxxx","userGroup":[{"groupName":"nodeJs","groupAssigned":"No","_id":"56b086ccd1bc72eb21d99c62"},{"0":{"groupName":"php","groupAssigned":"No","_id":"56b0880d13c9fc4c22a1d291"}},{"0":{"groupName":"android","groupAssigned":"No","_id":"56b0883513c9fc4c22a1d292"}},{"0":{"groupName":"ios","groupAssigned":"No","_id":"56b0883913c9fc4c22a1d293"}},{"0":{"groupName":"dotNet","groupAssigned":"No","_id":"56b0884213c9fc4c22a1d294"}},{"0":{"groupName":"testing","groupAssigned":"No","_id":"56b0885913c9fc4c22a1d295"}},{"0":{"groupName":"design","groupAssigned":"No","_id":"56b0886213c9fc4c22a1d296"}}]}

我想在html元素中安排数据,如:



<label>username</label> <label>groupName1</label> <input type = 'checkbox' checked /> <label>groupName2</label> <input type = 'checkbox' checked />
&#13;
&#13;
&#13;

..... 除了username,groupName和具有多个实例的复选框。如何将其添加到一个<li>代码

1 个答案:

答案 0 :(得分:1)

尝试:

var x = {"_id":"56af2ca2eb91bd4721443037","username":"xxxxxx","userGroup":[{"0":{"groupName":"nodeJs","groupAssigned":"No","_id":"56b086ccd1bc72eb21d99c62"}},{"0":{"groupName":"php","groupAssigned":"No","_id":"56b0880d13c9fc4c22a1d291"}},{"0":{"groupName":"android","groupAssigned":"No","_id":"56b0883513c9fc4c22a1d292"}},{"0":{"groupName":"ios","groupAssigned":"No","_id":"56b0883913c9fc4c22a1d293"}},{"0":{"groupName":"dotNet","groupAssigned":"No","_id":"56b0884213c9fc4c22a1d294"}},{"0":{"groupName":"testing","groupAssigned":"No","_id":"56b0885913c9fc4c22a1d295"}},{"0":{"groupName":"design","groupAssigned":"No","_id":"56b0886213c9fc4c22a1d296"}}]};
$('body').append('<label>'+x.username+'</label>');
$.each(x.userGroup,function(i,v){
 $('body').append('<label>'+v["0"].groupName+'</label> <input type ="checkbox" checked />');
 console.log(v)
});

https://jsfiddle.net/4vxxenk3/1/

或没有修改过的json:

https://jsfiddle.net/4vxxenk3/2/