简体   繁体   中英

Cant convert JSON to array in Angular.js

Hi everyone I have some problems with angular, I made a request in $http so i have a JSON like:

{"y":"1","a":"0"}

and I want to convert it to an array like

{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

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.

Use this method is used to convert json string response to an array, object, number

    var temp = [];
    temp = angular.fromJson(json); 
    console.log(temp);

This function will convert and return

https://docs.angularjs.org/api/ng/function/angular.fromJson

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.

Loop though the keys and convert the values:

for (key in obj) obj[key] = parseInt(obj[key], 10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM