1

how can I loop through this JSOn object and get the data in an unordered list like

.fnem anam 1

.ynem snam 2

.onem dnam 3

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

for more clarity,this is the source of that result:

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

3 Answers3

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 version:

$.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]);
})
Emil Condrea
  • 9,705
  • 7
  • 33
  • 52
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]);
}
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

try this:

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.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • Depending on the order that properties will be visited by `for-in` seems like a bad idea. – Barmar Feb 03 '14 at 10:09
  • There's no guarantee that the'll be processed in a particular order. – Barmar Feb 03 '14 at 10:11
  • See this question: http://stackoverflow.com/questions/21521410/stringify-an-js-object-in-asc-order I assume JSON.stringify is internally looping over the properties, and he's running into cases where it reorders them. – Barmar Feb 03 '14 at 10:15
  • but in this question json stringify is not the issue? OP has given json string and he ask how to iterate. – Zaheer Ahmed Feb 03 '14 at 10:16
  • Internally, stringify presumably has a loop similar to yours. And that loop doesn't get the elements in the order that the programmer put them. Javascript object properties are not ordered, and you shouldn't depend on them to remember the order they were entered. – Barmar Feb 03 '14 at 10:18