如何将Query字符串参数解码为JSON对象

时间:2014-10-02 12:33:48

标签: javascript jquery json

如何将Query字符串参数转换为JSON对象

我编写的代码

var actualappt = {
    RoomId: 1, HotelId: 29, BookingStartDate: Sept 26 2014, BookingEndDate: Sept 26 2014 , BookingStatusId: '1', BookingTypeId: '1', DepositAmount: 3000, NoOfRooms: 2, UnitPrice: 4000,
    Customers: { GuestName: rk, MobileNo: 5656566555, Email: rk@gmail.com, Address: hyd }, Payment_Mode_Id: 4
};

var stringsata = JSON.stringify(actualappt);

var queryString = $.param(actualappt);

现在我能够将JSON对象转换为查询字符串并发送到下一页,在我的页面中我想要反向过程,即从查询字符串到JSON对象

1 个答案:

答案 0 :(得分:1)

var actualappt = {
    RoomId: 1, HotelId: 29, BookingStartDate: "Sept 26 2014", BookingEndDate:"Sept 26 2014" , BookingStatusId: '1', BookingTypeId: '1', DepositAmount: 3000, NoOfRooms: 2, UnitPrice: 4000,
    Customers: { GuestName: "rk", MobileNo: 5656566555, Email: "rk@gmail.com", Address: "hyd"}, Payment_Mode_Id: 4
};

var stringsata = JSON.stringify(actualappt);

var queryString = $.param(actualappt);
var recoveredParams = {};
var queryString = decodeURIComponent(queryString)
$.each(queryString.split('&'), function(key, value){
    var item= value.split('=');
    recoveredParams[item[0]] = item[1]; 
});
console.log(recoveredParams)