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
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
Share
Improve this question
edited May 25, 2017 at 14:10
Abraham Murciano Benzadon
1,72318 silver badges35 bronze badges
asked May 25, 2017 at 10:09
vaishali bagulvaishali bagul
612 gold badges2 silver badges7 bronze badges
3
- 1 It's unclear what you're asking. Show us your expected output. – Mitya Commented May 25, 2017 at 10:13
-
1
have u even tried to search on stackoverflow..it asked many times.. try if it works for you
encodeURIComponent(JSON.stringify("http://localhost:63342/AngularJs/services/e_cell.html#!/%7B%22book%22:%22ABC%22%7D"))
– Dhara Commented May 25, 2017 at 10:14 - This code again encode the URL !!@Dhara – vaishali bagul Commented May 25, 2017 at 10:27
2 Answers
Reset to default 8As 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 ponent 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
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);