最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON to Array react, jquery - Stack Overflow

programmeradmin5浏览0评论

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.

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 edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Aug 4, 2015 at 1:13 Tristán BelmontTristán Belmont 731 gold badge2 silver badges9 bronze badges 2
  • post your server side script – VMcreator Commented Aug 4, 2015 at 1:15
  • Do you need anything more than this? var user = JSON.parse(responseJson).response.user; var putItInAnArrayLikeThis = [user]; – widged Commented Aug 4, 2015 at 14:28
Add a ment  | 

2 Answers 2

Reset to default 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.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论