代码之家  ›  专栏  ›  技术社区  ›  ryvantage

防止Javascript对JSON重新排序

  •  1
  • ryvantage  · 技术社区  · 6 年前

    我有两个JSON映射:

    {
      "1000006": "Alternate Business Phone",
      "1000008": "Alternate Home Phone",
      "1000001": "Business Phone",
      "1000003": "Clinic Phone",
      "3": "Facsimile",
      "1000007": "Home Phone",
      "1000004": "Lab Phone",
      "4": "Pager",
      "1000002": "Secure Msg Phone"
    }
    

    {
      "6": "Business Email",
      "1000005": "Deliver To Email",
      "7": "Personal Email"
    }
    

    这些是按值字母顺序排列的键值映射。我使用这两个选项根据另一个下拉菜单的选定项更改下拉菜单的内容。图片说明:


    选定的电话 :

    enter image description here


    选定的电子邮件 :

    enter image description here


    但是,正如您在图像中看到的,列表项的顺序没有被保留。

    我处理列表项的Javascript如下所示:

      var options_1 = {"1000006":"Alternate Business Phone","1000008":"Alternate Home Phone","1000001":"Business Phone","1000003":"Clinic Phone","3":"Facsimile","1000007":"Home Phone","1000004":"Lab Phone","4":"Pager","1000002":"Secure Msg Phone"};
      var options_2 = {"6":"Business Email","1000005":"Deliver To Email","7":"Personal Email"};
      function changePhoneEmailItems()
      {
        var selectedItem = document.getElementById("addNewCategory").value;
        var options;
        if('1' === selectedItem) {
            options = options_1;
        } else {
            options = options_2;
        }
        var list = document.getElementById("addNewUsageType");
        list.options.length=0;
        for(var i in options) {
            list.add(new Option(options[i], i));
        }
      }
    

    根据 this StackOverflow answer ,我需要更改我使用的数据结构,因为根据定义,JSON是无序的。

    new Map :

    var options_1 = {"1000006":"Alternate Business Phone","1000008":"Alternate Home Phone","1000001":"Business Phone","1000003":"Clinic Phone","3":"Facsimile","1000007":"Home Phone","1000004":"Lab Phone","4":"Pager","1000002":"Secure Msg Phone"};
    var options_2 = {"6":"Business Email","1000005":"Deliver To Email","7":"Personal Email"};
    var options_1_map = new Map(options_1);
    console.log(options_1_map);
    

    但它抛出了一个错误:

    TypeError: options_1 is not iterable
    

    注意:我的JSON来自一个Spring控制器,它正在用JSON“包装”映射以发送到视图。但是,如果有更合理的方法将贴图从控制器传输到视图,我可以修改控制器。

    编辑:

    for(int i = 1; i <= 2; i++) {
        JsonObject json = new JsonObject();
        HashMap<String, String> CDCONTMETHTP = (HashMap<String, String>)model.get("CDCONTMETHTP_" + i);
        for(Entry<String, String> option : CDCONTMETHTP.entrySet()) {
            json.addProperty(option.getKey(), option.getValue());
        }
        model.put("options_" + i, json);
    }
    

    如果需要,我可以对其进行修改。

    3 回复  |  直到 6 年前
        1
  •  6
  •   Davi    6 年前

    Array s、 哪个 维持秩序。

    代替此JSON:

    {
      "6": "Business Email",
      "1000005": "Deliver To Email",
      "7": "Personal Email"
    }
    

    [
      {"id": 6, "text": "Business Email"},
      {"id": 1000005, "text": "Deliver To Email"},
      {"id": 7, "text": "Personal Email"}
    ]
    
        2
  •  0
  •   jusdepatate LuisM    6 年前

    Like someone said here ; 放 _ 在数字之前。

    祝你今天愉快

        3
  •  0
  •   ryvantage    6 年前

    David的答案是有效的,但必须修改遍历元素的for循环。我已在此处发布了完整的解决方案:

    var options_1 = {"1000006":"Alternate Business Phone","1000008":"Alternate Home Phone","1000001":"Business Phone","1000003":"Clinic Phone","3":"Facsimile","1000007":"Home Phone","1000004":"Lab Phone","4":"Pager","1000002":"Secure Msg Phone"};
    var options_2 = {"6":"Business Email","1000005":"Deliver To Email","7":"Personal Email"};
    function changePhoneEmailItems()
    {
        var selectedItem = document.getElementById("addNewCategory").value;
        var options;
        if('1' === selectedItem) {
            options = options_1;
        } else {
            options = options_2;
        }
    
        var list = document.getElementById("addNewUsageType");
        list.options.length=0;
        Object.keys(options).forEach(function(i) {
            list.add(new Option(options[i].value, options[i].key))          
        });
    }