How to convert an entity to Json?

How can I convert JSON format string into a real object in JS?

  • I am using AngularJS. I get JSON format string from Server using RESTful, but now I want to work with my json response as a real js object I can transfer to other pages and do even more. How to do that? Is there a good framework that does it for me? My object is complicated, not a simple one.

  • Answer:

    Angularjs web site do...

Jim Hatton at Quora Visit the source

Was this solution helpful to you?

Other answers

These are available methods AFAIK. Choose one from these Use JSON.parse JSON.parse(stringifiedObject) Use new function, var parsed = new Function('return ' + stringifiedObject)(); use eval, eval(stringifiedObject) I like the beauty of new Function though it has some minor side effects :).

Prathap Reddy

you can use javascript's parse function. var jsObject = JSON.parse(jsonString);

Levan Gulisashvili

If you are looking for an application to do it, there is https://itunes.apple.com/us/app/textlab/id1024903185?ls=1&mt=12 for Mac OS which has a feature to convert JSON into JavaScript object.

Ondrej Kvasnovsky

I will give the answer assuming you are using jQuery. var result = null; $.ajax({ type: 'post', url: '/result.php', data: "gimme some json dude", dataType: 'json', success: function(res){ result = res; // Now this $result is a member of the window object and can accessed globally. You can test it for null. If null, it doesn't hold value else it does. } }); //Suppose you want to parse it var parsed_result = JSON.parse(result); Hope this helps. Good Luck!

Christian M Raymonds

Nothing if you are using the $http service. The default response transform function of the service can automatically detect JSON and parse it into an object in the data variable of the success handler.

Arjun Mathai

You should return a json response from your API instead of a string. You should be able to set the content type of your response to JSON and then you will not have to parse it to JSON object.

Imad Hashmi

You could just use AngularJS: var jsObj = angular.fromJSON(<your_json_string>); Reference: https://docs.angularjs.org/api/ng/function/angular.fromJson or use proper browser json parse: var jsObj = JSON.parse(<you_json_string>); Reference: http://www.w3schools.com/json/json_eval.asp

Diego Moreira

You can use the gson api to do this. in java: for converting json to java object use: fromJson(jsonObject,javaclassname.class) for converting java object to json object use: toJson(javaObject) in javascript - use stringify method to convert the json to string, and parse(jsonstring) to convert the string to json

Mohammed Nazaar

You can just use JSON.parse() as other answers suggest here but if you are using AngularJS take a look at https://github.com/mgonto/restangular a very good rest service that simplifies everything. and will give you restangular object as the result which contains more than just a parsed JSON object.

S K Amarnath

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.