1

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

2
  • BTW that is an object not Array. Commented Mar 25, 2015 at 3:10
  • what is the purpose? what do you want to do? explain clearer ... Commented Mar 25, 2015 at 3:11

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.