简体   繁体   English

如何将编码的网址转换为JSON格式?

[英]How to convert Encoded url into JSON format?

Here I want to convert my encoded url into JSON format. 在这里,我想将编码后的网址转换为JSON格式。 The encoded url is: 编码后的网址为:

http://localhost:63342/AngularJs/services/e_cell.html#!/%7B%22book%22:%22ABC%22%7D

As much as I understand from your URL you are trying to post this %7B%22book%22:%22ABC%22%7D data in query string. 据我从您的URL所了解,您正在尝试在查询字符串中发布此%7B%22book%22:%22ABC%22%7D数据。

So first you need to decode your URL encoded data into an string which can be parsed. 因此,首先您需要将URL编码的数据解码为可以解析的字符串。 For that you can take help of decodeURIComponent() javascript API. 为此,您可以使用decodeURIComponent() JavaScript API。

decodeURIComponent() - this function decodes an encoded URI component back to the plain text ie like in your encoded text it will convert %7B into opening brace { . decodeURIComponent() -此函数将已编码的URI组件解码回纯文本,即像在您的已编码文本中一样,它将%7B转换为大括号{ So once we apply this API you get - 因此,一旦我们应用此API,您将获得-

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

This is a valid JSON string now you can simply parse. 这是一个有效的JSON字符串,现在您可以轻松解析。 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 JSON.parse()方法解析一个JSON字符串,构造该字符串描述的JavaScript值或对象

The decodeURIComponent function will convert URL encoded characters back to plain text. decodeURIComponent函数会将URL编码的字符转换回纯文本。

var myJSON = decodeURIComponent("%7B%22book%22:%22ABC%22%7D");
var myObject = JSON.parse(myJSON);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM