Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Encoded url into JSON format?

Here I want to convert my encoded url into JSON format. The encoded url is:

http://localhost:63342/AngularJs/services/e_cell.html#!/%7B%22book%22:%22ABC%22%7D
like image 764
vaishali bagul Avatar asked Sep 06 '25 22:09

vaishali bagul


2 Answers

As much as I understand from your URL you are trying to post this %7B%22book%22:%22ABC%22%7D data in query string.

So first you need to decode your URL encoded data into an string which can be parsed. For that you can take help of decodeURIComponent() javascript API.

decodeURIComponent() - this function decodes an encoded URI component back to the plain text i.e. like in your encoded text it will convert %7B into opening brace {. So once we apply this API you get -

//output : Object { book: "ABC" }

This is a valid JSON string now you can simply parse. So what all you need to do is -

var formData = "%7B%22book%22:%22ABC%22%7D";
var decodedData = decodeURIComponent(formData);
var jsonObject = JSON.parse(decodedData);
console.log(jsonObject );

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string

like image 179
Naveen Kumar Avatar answered Sep 08 '25 12:09

Naveen Kumar


The decodeURIComponent function will convert URL encoded characters back to plain text.

var myJSON = decodeURIComponent("%7B%22book%22:%22ABC%22%7D");
var myObject = JSON.parse(myJSON);
like image 39
Quentin Avatar answered Sep 08 '25 13:09

Quentin