2

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.

2
  • post your server side script Commented Aug 4, 2015 at 1:15
  • Do you need anything more than this? var user = JSON.parse(responseJson).response.user; var putItInAnArrayLikeThis = [user]; Commented Aug 4, 2015 at 14:28

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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: ""}]

1 Comment

i cant modify the server side script

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.