Javascript 无法在 Angular.js 中将 JSON 转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29246725/
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
Cant convert JSON to array in Angular.js
提问by Jess Verduzco
Hi everyone I have some problems with angular, I made a request in $http so i have a JSON like:
大家好,我在 angular 方面遇到了一些问题,我在 $http 中提出了一个请求,所以我有一个 JSON,如:
{"y":"1","a":"0"}
{"y":"1","a":"0"}
and I want to convert it to an array like
我想把它转换成一个数组
{y:1, a:0}
{y:1, a:0}
I'd already tried whit angular.fromJson(myData) but it doesnt works Hope you′ll help me because Im a begginer
我已经尝试过 angular.fromJson(myData) 但它不起作用希望你能帮助我,因为我是一个初学者
回答by Brian
This is just a hint/suggestion, something you might overlooked. It is hard to understand your question, clarify if this is not what you are looking for.
这只是一个提示/建议,您可能会忽略。很难理解你的问题,如果这不是你想要的,请澄清。
Maybe for some reason you got "{"y":"1","a":"0"}"back, which is a string.
If you need to get a JSON object from it then use JSON.parse(string)function.
也许出于某种原因你"{"y":"1","a":"0"}"回来了,这是一个字符串。如果您需要从中获取 JSON 对象,请使用JSON.parse(string)函数。
回答by Hina Vaja
Use this method is used to convert json string response to an array, object, number
使用此方法用于将json字符串响应转换为数组、对象、数字
var temp = [];
temp = angular.fromJson(json);
console.log(temp);
This function will convert and return
此函数将转换并返回
回答by Guffa
The only difference between the object that you have after parsing the JSON and the object that you want is that the string values need to be number values.
解析 JSON 后您拥有的对象与您想要的对象之间的唯一区别是字符串值必须是数字值。
Loop though the keys and convert the values:
通过键循环并转换值:
for (key in obj) obj[key] = parseInt(obj[key], 10);

