Reputation: 35
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
Upvotes: 1
Views: 35353
Reputation: 324
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
Upvotes: 4
Reputation: 700730
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);
Upvotes: 0
Reputation: 5028
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.
Upvotes: 6