Android 如何按顺序对json对象键排序?

Android 如何按顺序对json对象键排序?,android,json,Android,Json,这是我的json结构 { "menus": [ { "sequence":"1", "Name":"Sector" }, { "sequence":"3", "Name":"Segment" }, { "sequence":"2", "Name":"Collection" } ] } 如何按顺序解析/存储像1、2、3这样的数据 创建一个对象类并初始化变量,如下面的be

这是我的json结构

{
  "menus": [
    {
      "sequence":"1",
      "Name":"Sector"
    },
    {
      "sequence":"3",
      "Name":"Segment"
    },
    {
      "sequence":"2",
      "Name":"Collection"
    }
  ]
}
如何按顺序解析/存储像1、2、3这样的数据

  • 创建一个对象类并初始化变量,如下面的bellow和apply getter和setter 类菜单 {

    私有字符串序列

    私有字符串名称

    公共字符串getSequence() {

    }

    公共void集合序列(字符串序列) {

    公共字符串getName() {

    公共void集合名(字符串名) {

    }

    2.然后像下面那样解析Json值

  • 将列表初始化为菜单列表
  • List menuslist=new ArrayList()

    试一试{

    JSONObject jObject=新的JSONObject(result.toString)

    JSONArray数组=jObject.getJSONArray(“菜单”)

    对于(int i=0;i JSONObject Object=array.JSONObject(i)

    菜单=新菜单()

    menus.setSequence(Object.getString(“sequence”)

    menus.setName(Object.getString(“name”)

    菜单列表。添加(菜单)

    }

    }

    捕获(JSONException Je) {

    je.例外情况


    }在列表中解析数组

    JSONArray sortedJsonArray = new JSONArray();
    List<JSONObject> jsonList = new ArrayList<JSONObject>();
    for (int i = 0; i < jsonArray.length(); i++) {
        jsonList.add(jsonArray.getJSONObject(i));
    }
    
    JSONArray sortedJsonArray=newjsonarray();
    List jsonList=new ArrayList();
    for(int i=0;i
    然后使用collection.sort对新创建的列表进行排序

    Collections.sort( jsonList, new Comparator<JSONObject>() {
    
        public int compare(JSONObject a, JSONObject b) {
            String valA = new String();
            String valB = new String();
    
            try {
                valA = (String) a.get("sequence");
                valB = (String) b.get("sequence");
            } 
            catch (JSONException e) {
                //do something
            }
    
            return valA.compareTo(valB);
        }
    });
    
    Collections.sort(jsonList,newcomparator(){
    公共int比较(JSONObject a、JSONObject b){
    String valA=新字符串();
    String valB=新字符串();
    试一试{
    valA=(字符串)a.get(“序列”);
    valB=(String)b.get(“序列”);
    } 
    捕获(JSONException e){
    //做点什么
    }
    返回valA.compareTo(valB);
    }
    });
    
    在数组中插入排序后的值

    for (int i = 0; i < jsonArray.length(); i++) {
        sortedJsonArray.put(jsonList.get(i));
    }
    
    for(int i=0;i
    我使用TreeMap

    Map sequenceMenuOrder = new TreeMap<String, String>();
                                        int position = 0;
                                        for (int index = 0; index < menus.length(); index++) {
                                            sequenceMenuOrder.put(menus.getJSONObject(index).getString("sequence"), menus.getJSONObject(index).getString("menuName"));
                                        }
                                        for (Object value : sequenceMenuOrder.keySet()) {
                                            if (position != 0) {
                                                menuString += ",";
                                            }
                                            menuString += sequenceMenuOrder.get(value);
                                            position++;
                                        }
    
    Map sequenceMenuOrder=new TreeMap();
    int位置=0;
    对于(int index=0;index
    数据来自哪里?json数据来自服务器。我想显示菜单,它们是按顺序提到的。将它们存储在ArrayList中并对列表进行排序。请服务器人员按排序顺序发送数据,这将有助于所有客户端(如安卓、iOS),并且您的排序代码保留在一个位置。这是错误的,您应该比较n数值,而不是字符串-这将按1,10100,2,20200等进行排序
    JSONArray sortedJsonArray = new JSONArray();
    List<JSONObject> jsonList = new ArrayList<JSONObject>();
    for (int i = 0; i < jsonArray.length(); i++) {
        jsonList.add(jsonArray.getJSONObject(i));
    }
    
    Collections.sort( jsonList, new Comparator<JSONObject>() {
    
        public int compare(JSONObject a, JSONObject b) {
            String valA = new String();
            String valB = new String();
    
            try {
                valA = (String) a.get("sequence");
                valB = (String) b.get("sequence");
            } 
            catch (JSONException e) {
                //do something
            }
    
            return valA.compareTo(valB);
        }
    });
    
    for (int i = 0; i < jsonArray.length(); i++) {
        sortedJsonArray.put(jsonList.get(i));
    }
    
    Map sequenceMenuOrder = new TreeMap<String, String>();
                                        int position = 0;
                                        for (int index = 0; index < menus.length(); index++) {
                                            sequenceMenuOrder.put(menus.getJSONObject(index).getString("sequence"), menus.getJSONObject(index).getString("menuName"));
                                        }
                                        for (Object value : sequenceMenuOrder.keySet()) {
                                            if (position != 0) {
                                                menuString += ",";
                                            }
                                            menuString += sequenceMenuOrder.get(value);
                                            position++;
                                        }