2

I have a JSON object in which I have some arrays with keys:

{  
  "body":{  
    "menus":{  
      "":[  
        {  
          "name":"home",
          "label":"Home",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_global_search",
          "label":"Search",
          "headerLabel":"",
          "icon":"",
          "url":""
        }
      ],
      "Favorites":[  
        {  
          "name":"core_mini_messages",
          "label":"Messages",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_mini_notification",
          "label":"Notifications",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_mini_friend_request",
          "label":"Friend Requests",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_user",
          "label":"Members",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_album",
          "label":"Albums",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_video",
          "label":"Videos",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_blog",
          "label":"Blogs",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_classified",
          "label":"Classifieds",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_group",
          "label":"Groups",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_event",
          "label":"Events",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"core_main_music",
          "label":"Music",
          "headerLabel":"",
          "icon":"",
          "url":""
        }
      ],
      "Account Settings":[  
        {  
          "name":"user_settings",
          "label":"Settings",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"contact_us",
          "label":"Contact Us",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"privacy_policy",
          "label":"Privacy Policy",
          "headerLabel":"",
          "icon":"",
          "url":""
        },
        {  
          "name":"terms_of_service",
          "label":"Terms Of Service",
          "headerLabel":"",
          "icon":"",
          "url":""
        }
      ],
      "Help & Settings":[  
        {  
          "name":"signout",
          "label":"Sign Out",
          "headerLabel":"",
          "icon":"",
          "url":""
        }
      ]
    },
    "languages":{  
      "default":"en",
      "languages":{  
        "en":"English"
      }
    }
  }
}

I want to iterate over the elements in the same order that they are coming from server, but the keys change their order automatically alphabetical.

Any ideas?

Drenmi
  • 8,492
  • 4
  • 42
  • 51
Michael
  • 39
  • 3

2 Answers2

0

My solution to the problem would be to send another object through that has a key=>value association with numbers as the key and the menu items as the values, e.g.

{1:"", 2:"Favorites", 3:"Account Settings", 4:"Help & Settings"}

I would then iterate through this object and use it to retrieve the menus from the original object in the right order.

Skytiger
  • 1,745
  • 4
  • 26
  • 53
0

Looking at previous answers there seem to be two ways to solve the problem:

  1. Restructure your data, change menues to look like this:

    {
      "menues": [
                 {"": ... },
                 {"Favorites": ... },
                 ...]}
    
  2. Change your JSONObject.java file it uses a HashMap but a LinkedHashMap should preserve the order. This will only work in systems that use your JSONObject.java though.

    //this.map = new HashMap();
    this.map = new LinkedHashMap();
    

Both solution I got from there: Keep the order of the JSON keys during JSON conversion to CSV for better explanations.

Community
  • 1
  • 1
ralgrad
  • 29
  • 10