2

How to convert the query string parameters to a JSON object

Code I was written

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: [email protected], Address: hyd }, Payment_Mode_Id: 4
};
var stringsata = JSON.stringify(actualappt);
var queryString = $.param(actualappt);

Now I am able to convert JSON object to query string and sending to next page, in my page I want reverse process i.e from query string to JSON object

asked Oct 2, 2014 at 12:33
3
  • 1
    FYI: That is not JSON. Commented Oct 2, 2014 at 12:35
  • @epascarello Correct. This is a normal object. Commented Oct 2, 2014 at 12:36
  • see stackoverflow.com/questions/1131630/… Commented Oct 2, 2014 at 12:48

1 Answer 1

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: "[email protected]", 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)
Adrian Forsius
1,4562 gold badges21 silver badges30 bronze badges
answered Oct 2, 2014 at 12:46

3 Comments

i modify "actualappt" to correctly Javascript Object
If I convert recoveredParams to JSON.stringify(actualappt) Customers[GuestName]:rk like this getting , is it possible to convert " Customers: { GuestName: "rk", MobileNo: 5656566555, Email: "[email protected]", Address: "hyd"}" like this @foued611
it only posible is valid JS Object like this: { Customers: { GuestName: "rk", MobileNo: 5656566555, Email: "[email protected]", Address: "hyd"}}

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.