JS implementation of a query string to JSON format data method

Source: Internet
Author: User
Tags string to json

The format of the input string is a=1&b=2&c=3

$.par2json = function (string, overwrite) {
var obj = {}, pairs = String.Split (' & '), d = decodeuricomponent, name, value;
$.each (pairs, function (I, pair) {
Pair = pair.split (' = ');
Name = d (Pair[0]);
Value = d (pair[1]);
Obj[name] = value;
});
return obj;
};

var test=$.par2json (' a=1&b=2&c=3 ');
Console.log (test);

Object {A: "1", B: "2", C: "3"}

JSON object to switch to JSON string:

var jsonobj=json.stringify (test);

JSON string to JSON object

Json.parse (Jsonobj);

JS implementation of a query string to JSON format data method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.