How to change the order of the fields in JSON

后端 未结 6 908
孤城傲影
孤城傲影 2020-12-01 17:13

Scenario: Consider I have a JSON documents as follows:

   {
     \"name\": \"David\",
     \"age\" : 78,
     \"NoOfVisits\" : 4
   }
         


        
6条回答
  •  天命终不由人
    2020-12-01 17:36

    As already noted, JavaScript object properties have no guaranteed order (think of it, does it make any sense to have one?).

    If you really need to have an order, you could use an array as:

    [{"name" : "Dave"}, {"age" : 78}, {"NoOfVisits" : 4}]
    

    or:

    ["Dave", 78, 4] // but here you have no idea what is happening
    

    I wouldn't want to see any of these in my code. So a better idea is to re-think what you are trying to achieve.

提交回复
热议问题