如何循环单个字符串JSON

时间:2014-02-03 09:57:15

标签: jquery json

如何遍历此JSOn对象并将数据放入无序列表中,如

.fnem anam 1

.ynem snam 2

.onem dnam 3

{"uname":["fnem,ynem,onem"],"Oname":["anam,snam,dnam"],"osize":["1,2,3"]} 

为了更清楚,这是该结果的来源:

echo json_encode(array('uname'=>$_POST['Oname'],'Oname'=>$_POST['uname'],'osize'=>$_POST['size']));

3 个答案:

答案 0 :(得分:0)

var mobject = {"uname":["fnem,ynem,onem"],"Oname":["anam,snam,dnam"],"osize":["1,2,3"]} 
for(var topV in mobject)
{
   mobject[topV] = mobject.split();
}
for(var value in mobject.uname){
    console.log(mobject.uname[value],mobject.Oname[value],mobject.osize[value]);
}

jquery版本:

$.each(mobject,function(index,value){
    mobject[index] = mobject.split();
})

$.each(mobject.uname,function(index,value){
    console.log(mobject.uname[index],mobject.Oname[index],mobject.osize[index]);
})

答案 1 :(得分:0)

var jObject = {
"uname": ["fnem, ynem, onem"],
"Oname": ["anam, snam, dnam"],
"osize": ["1, 2, 3"]
};
var sunames = jObject.uname[0].split(",");
var sonames = jObject.Oname[0].split(",");
var sosizes = jObject.osize[0].split(",");

for(var i = 0; i < sunames.length; i++)
{
    console.log(sunames[i] + ' ' + sonames[i] + ' ' + sosizes[i]);
}

答案 2 :(得分:0)

试试这个:

var data = {
    "uname": ["fnem,ynem,onem"],
    "Oname": ["anam,snam,dnam"],
    "osize": ["1,2,3"]
};

for (var i = 0; i < 3; i++) {
    var str="";
    for (var property in data) {
        if (data.hasOwnProperty(property)) {
            str+=" "+data[property][0].split(',')[i];
        }
    }
    alert(str);
}

Here is demo.