简体   繁体   English

如何在Java中将json数组转换为有序集合

[英]how to convert json array to an ordered collection in java

I have a scenario in java web service to accept json object as input.Here json object doesn't ensure order.so is there any provision to convert this input json object to an ordered collection in java before reading it? 我在Java Web服务中有一个方案接受json对象作为输入。这里json对象不能确保order.so是否有任何规定java before读取它java before将输入的json对象转换为java before的有序集合?

@Post @邮政

public Representation updateTestData(JSONObject entity) {
    Representation rep = null;
    JSONObject reqText;
    try {
        reqText = entity;
  //when i'm reading the json object. attributes and  values of json are not in order as that in submit
        log.info("input data: " + reqText);         
                } catch (Exception e) {
        e.printStackTrace();
        log.error("EXCEPTION CAUGHT WHILE UPDATETESTDATA: " + getRequest().getResourceRef().toString()
                + " message: " + e.getMessage());
    }
    return rep;
}

If you can modify the json structure, change it like this: [{key:value},{key2:value},...] , this ensures the order. 如果您可以修改json结构,请像这样更改它: [{key:value},{key2:value},...] ,这样可以确保顺序。 But if not, then you have to put an additional array to store the order. 但是,如果没有,则必须放置一个额外的数组来存储订单。 it looks dumb though. 虽然看起来很蠢。 hope it helps. 希望能帮助到你。

在Json Object中,Element未按排序顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM