javascript JSON 到数组的反应,jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31799161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON to Array react, jquery
提问by Tristán Belmont
I have json data obtained form my server and i want to put in a list, i already read Convert JSON To Array Javascriptand Converting data from JSON to JavaScript arraybut i can't figure out what of all the options can i use in my code my json is like this:
我从我的服务器获得了 json 数据,我想放入一个列表中,我已经阅读了 Convert JSON To Array Javascript和Converting data from JSON to JavaScript array,但我无法弄清楚我可以在我的所有选项中使用哪些选项代码我的json是这样的:
{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, "success": true}
{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, “成功”:真}
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
我能为此做什么?我已经尝试了这个答案: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.
但是第二个不显示任何内容也不显示任何错误,第一个显示未定义的错误 arr 未定义。
回答by Mike Brant
Assuming your JSON is like this as posted:
假设你的 JSON 是这样的:
{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, "success": true}
You already have the exact data structure you want at .response.user
.
您已经拥有所需的确切数据结构.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.
这根本不需要任何转换(除了反序列化 JSON 字符串),您只需提取 JSON 中表示的数据结构的一部分。
回答by VMcreator
In your sever side script the array that will be converted to json should look like this:
在您的服务器端脚本中,将转换为 json 的数组应如下所示:
$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:
使用 javascript 转换为 json 后,输出将完全是:
user:[{username: "",description: "", picture_url: "", name: "", phone: ""}]
用户:[{用户名:“”,描述:“”,图片网址:“”,姓名:“”,电话:“”}]