Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert AJAX String response to JSON

My EjB response using AJAX likes this:

"{'Active':{'123','456','789'},'Inactive':{'111','222','333'}}"

I want to convert the above string to JSON objects. JSON objects will be used in JavaScript to draw the map.

Or

I want the same string to get converted like below in JavaScript:

var active = ["123", "456", "789"]; var inactive = ["111", "222", "333"];

Note: I'm using AJAX in JS.

like image 706
Murugesh Avatar asked Oct 13 '17 01:10

Murugesh


People also ask

How do I get AJAX response in JSON format?

Approach: To solve this problem, we will first consider a JSON file named “capitals. json” and try to get this JSON data as a response using AJAX. Then we will create an HTML file “capitals. html” which contains a table which we will use to populate the data we are getting in response.

How do I convert a string to JSON?

String data can be easily converted to JSON using the stringify() function, and also it can be done using eval() , which accepts the JavaScript expression that you will learn about in this guide.

Does AJAX return JSON?

In this tutorial, I showed how you can return the JSON response and handle it in jQuery AJAX. You can convert the PHP array in JSON format with json_encode() function and return as a response. Set dataType: 'JSON' when send AJAX request.


1 Answers

var jsonString = JSON.parse(response);

response is what you get from ajax req (a json string), and jsonString is what you wanted

like image 111
qadirpervez Avatar answered Sep 30 '22 05:09

qadirpervez