0

I am using json for the first time so this may be a bit of a stupid question.

My PHP code for returning json is as follows:

$query="SELECT * FROM user_db WHERE rest_id='$rest_id'";
$result=mysqli_query($dbc, $query);
$row=array();
while($r = mysqli_fetch_assoc($result)) {
    $rows{'Users'][] = $r;
}
echo json_encode($rows);

This returns the following:

{"Users":
[{"user_id":"1361453832p3y","name":"","username":"sideshow","password":"sideshow","user_type":"1","rest_id":"1361453832fxL","email":""},
{"user_id":"1361523362ANq","name":"Sharon","username":"Sharon45","password":"Sharon45","user_type":"3","rest_id":"1361453832fxL","email":""},
{"user_id":"1361523653SXp","name":"Heather F","username":"fishface","password":"golliwog","user_type":"3","rest_id":"1361453832fxL","email":""}]}

All I am trying to do is to loop out the results and append them to the page.

My current JQuery is:

var name="";
        var username="";
        var password="";
        var user_type="";
        var output="";
        var obj=$.parseJSON(html);

        $.each(obj, function(){
            output+="<p><strong>"+this['name']+"</strong></p>";
            output+="<p>Username: "+this['username']+" Password: "+this['password']+"</p>";
            output+="<hr />";
        });


        $('.user_holder').html(output);

This just echos out each field three times. I have not found a way to loop through each json field.....

Sideshow
  • 1,321
  • 6
  • 28
  • 50

7 Answers7

3

You can try this:

var name="";
var username="";
var password="";
var user_type="";
var output="";

$.each($.parseJSON(html).Users, function(){
    output+="<p><strong>"+this['name']+"</strong></p>";
    output+="<p>Username: "+this['username']+" Password: "+this['password']+"</p>";
    output+="<hr />";
});

As you see the only difference is in the $.each method where we pass the Users property instead of the whole object.

Here is the output

<p><strong></strong></p><p>Username: sideshow Password: sideshow</p><hr /><p><strong>Sharon</strong></p><p>Username: Sharon45 Password: Sharon45</p><hr /><p><strong>Heather F</strong></p><p>Username: fishface Password: golliwog</p><hr />
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
1

change this

$.each(obj, function(){

to this:

$.each(obj.Users, function(){
Oden
  • 756
  • 8
  • 21
0

You need to loop through each JSON object and reference an index containing the data and the reference the key.

for example

 obj['Users'][0]['Username']

will return the first instance of username.

so you will need to use a nested for loop to loop through the data.

Roy James Schumacher
  • 636
  • 2
  • 11
  • 27
0

If I understand you correctly, you are sooo close to nailing it. I think you are just not referencing the Users field in obj.

$.each(obj.Users, function(){
    output+="<p><strong>"+this['name']+"</strong></p>";
    output+="<p>Username: "+this['username']+" Password: "+this['password']+"</p>";
    output+="<hr />";
});

console.log is your friend :) If you did

console.log(obj)

you could inspect what obj was, and see it has a Users field, and you'd be laughing :)

To learn about console.log, see this question: What is console.log?

Community
  • 1
  • 1
Mike Hogan
  • 9,933
  • 9
  • 41
  • 71
0

try

$.each(json,function(i,j){
    $(j).each(function(k,v){    
     console.log(v.user_id);
     console.log(v.username);        
    });
});

demo

Dakait
  • 2,531
  • 1
  • 25
  • 43
0

try:

$.each(obj.Users, function(){
        output+="<p><strong>"+this['name']+"</strong></p>";
        output+="<p>Username: "+this['username']+" Password: "+this['password']+"</p>";
        output+="<hr />";
    });
RedDevil79
  • 445
  • 5
  • 13
0

Have tried like this ?

         for(var i=0; i < obj.Users.length; i++){
            var username  =obj.Users[i].username;
                //TODO get remaining value of  obj.Users[i];

        } 
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307