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.
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.
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.
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.
var jsonString = JSON.parse(response);
response
is what you get from ajax req (a json string), and jsonString is what you wanted
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With