-2

Searched but did not get a proper solution.

I have a json string which is coming from the server. I want to convert the string to javascript array so that I can sort the data based on "hotel_name", 'minPrice', 'hotel_star'.

Here is the JSON string.

   {
   "00001065": {
      "hotel_id": "00001065",
      "hotel_name": "The Infantry Hotel",
      "hotel_star": "3",
      "image": "",
      "location": "Infantry Road",
      "minPrice": "2,497",
      "RoomTypes": [
         {
            "RoomTypeName": "Deluxe King / Twin Double",
            "AvailableQuantity": "4",
            "RatePlanInclusions": "Complimentary Wi-Fi Internet",
            "price": "2,497"
         },
         {
            "RoomTypeName": "Superior Double",
            "AvailableQuantity": "2",
            "RatePlanInclusions": "Breakfast",
            "price": "3,496"
         }
      ]
   },
   "00001080": {
      "hotel_id": "00001080",
      "hotel_name": "Hotel Ramanashree",
      "hotel_star": "3",
      "image": "",
      "location": "Richmond Road",
      "minPrice": "3,879",
      "RoomTypes": [
         {
            "RoomTypeName": "Executive Room",
            "AvailableQuantity": "25",
            "RatePlanInclusions": "Breakfast",
            "price": "3,879"
         },
         {
            "RoomTypeName": "Club Room",
            "AvailableQuantity": "25",
            "RatePlanInclusions": "Breakfast",
            "price": "4,604"
         }
      ]
   },
   "00003757": {
      "hotel_id": "00003757",
      "hotel_name": "The Paul ",
      "hotel_star": "5",
      "image": "",
      "location": "Domlur Layout",
      "minPrice": "6,216",
      "RoomTypes": [
         {
            "RoomTypeName": "Executive Suite - Two  Bedrooms Suite",
            "AvailableQuantity": "4",
            "RatePlanInclusions": "Complimentary Wi-Fi Internet, Breakfast",
            "price": "8,942"
         },
         {
            "RoomTypeName": "Premier Suite - Two  Bedrooms Suite",
            "AvailableQuantity": "2",
            "RatePlanInclusions": "Complimentary Wi-Fi Internet, Breakfast",
            "price": "10,718"
         },
         {
            "RoomTypeName": "Studio Suite - One  Bedroom Suite",
            "AvailableQuantity": "4",
            "RatePlanInclusions": "Complimentary Wi-Fi Internet, Breakfast",
            "price": "6,216"
         }
      ]
   }
}
  • This question has been the most favourite duplicate on SO. Pls use the top right corner to search for an answer with the exact same title of your question – MarsOne Nov 20 '14 at 07:27
  • 1
    possible duplicate of [Parse JSON in JavaScript?](http://stackoverflow.com/questions/4935632/parse-json-in-javascript) – MarsOne Nov 20 '14 at 07:29
  • @MarsOne The question you mentioned does not match with my question as my JSON string format is different. I already checked that question. Please check the both questions and try to differentiate them. – mrinmoy deka Nov 20 '14 at 07:33
  • @MarsOne If you think it is a duplicate of that question, I would request you give me a solution based on the answer of the other question. – mrinmoy deka Nov 20 '14 at 07:35
  • Buddy, you will not get a perfect solution. You hve to apply the logic into your code. Its like addition. If you know to do 2+2, then you should know to do 3+3. – MarsOne Nov 20 '14 at 07:44
  • 1
    I know buddy. I am also a developer. I could not solve the problem. That's why I posted the question here. – mrinmoy deka Nov 20 '14 at 07:47

4 Answers4

1

This is pretty standard JavaScript. Maybe you should try a JSON tutorial first? MDN is a good place to start.

You could just parse the JSON and convert it into an array.

// Converts an object into an array
function objectToArray(obj) {
    var array = [];
    for (prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            array.push(obj[prop]);
        }
    }
    return array;
}

var obj = JSON.parse(json_string);
var arr = objectToArray(obj);

You can now sort the array with sort() using your own compareFunction.

dusky
  • 1,133
  • 7
  • 12
0

You dont need to convert json object into a array to Sort. You can rather sort the JSON itself.

function sortResults(prop, asc) {
    arr = arr.sort(function(a, b) {
        if (asc) return (a[prop] > b[prop]);
        else return (b[prop] > a[prop]);
    });
}

I have created a working example with your Json. Click the headers to see the sort in this Example : http://jsfiddle.net/VAKrE/922/

Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45
  • Tried that too. The sort does not work with the variable 'arr'. – mrinmoy deka Nov 20 '14 at 07:43
  • Nice example, but OP asked about how to convert json object to an array so that it can be sorted. Your code shows how to sort an array. Not how to convert json object to an array (as shown in http://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key) – xmojmr Nov 20 '14 at 08:32
  • 1
    @RolwinC It worked for me with some little modifications. Thanks. – mrinmoy deka Nov 21 '14 at 06:12
0

You could do it this way:

var o = JSON.parse("...");

var asArray = Object.keys(o).map(function(k) { return o[k] });

asArray.sort(function(a, b) {
    if(a.hotel_name > b.hotel_name) return  1;
    if(a.hotel_name < b.hotel_name) return -1;
    return 0;
});
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
-1

var myObject = JSON.parse(jsonString);

sagie
  • 1,744
  • 14
  • 15