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.....

1
  • var obj=$.parseJSON(html); what contains html? Commented Feb 22, 2013 at 10:47

7 Answers 7

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 />
Sign up to request clarification or add additional context in comments.

Comments

1

change this

$.each(obj, function(){

to this:

$.each(obj.Users, function(){

Comments

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.

1 Comment

How do I get a count for the returned data ? This is where I seem to be stuck
0

try

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

demo

Comments

0

try:

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

Comments

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];

        } 

Comments

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?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.