Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have json data obtained form my server and i want to put in a list, i already read Convert JSON To Array Javascript and Converting data from JSON to JavaScript array but i can't figure out what of all the options can i use in my code my json is like this:

{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, "success": true}

and i want to put it on an array like this:

[{username: "",description: "", picture_url: "", name: "", phone: ""}]

what can i do for this? i already try the answers of this: Converting JSON Object into Javascript array

i also try some functions like this:

$.each(data, function(i,obj){
                $(arr).append(obj.username+'</br>');
                $(arr).append(obj.description+'</br>');
                $(arr).append(obj.name+'</br>');
            });

and this:

var results = data;
            var arr = [], item;
            for (var i = 0, len = results.length; i < len; i++) {
                item = results[i];
                arr.push({username: item.username, description: item.description, name: item.name});
            }

but with the second one dont show anything and dont show any error, with the first one, showme an unespecthed error arr is not defined.

share|improve this question
    
post your server side script – VMcreator Aug 4 '15 at 1:15
    
Do you need anything more than this? var user = JSON.parse(responseJson).response.user; var putItInAnArrayLikeThis = [user]; – widged Aug 4 '15 at 14:28

In your sever side script the array that will be converted to json should look like this:

$array=array("response"=>array(
                                   "user"=>array(array("username"=>"",
                                                       "description"=>"",
                                                       "picture_url"=>"",
                                                       "name"=>"",
                                                       "phone"=>"")),
                                   "success"=>true
                              )
             );
echo json_encode($array);

I think in your server side the "user" array format looks like

"user"=>array("username"=>"",
              "description"=>"",
              "picture_url"=>"",
              "name"=>"",
              "phone"=>"")

Instead of that, "user" array should be nested, like my first example.

"user"=>array(array("username"=>"",
                    "description"=>"",
                    "picture_url"=>"",
                    "name"=>"",
                    "phone"=>""))

After converting to json using javascript the output will exactly be:

user:[{username: "",description: "", picture_url: "", name: "", phone: ""}]

share|improve this answer
    
i cant modify the server side script – Tristán Belmont Aug 4 '15 at 16:20

Assuming your JSON is like this as posted:

{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, "success": true}

You already have the exact data structure you want at .response.user.

So code would be simple:

var json = '{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, "success": true}';
var obj = JSON.parse(json);
var theArrayYouWant = obj.response.user;

This doesn't require any transformation at all (other than deserializing the JSON string), you are just extracting a portion of the data structure represented in JSON.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.