有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Jackson数据绑定读取json三维数组

我想读一个json数组,如果可能的话,把它放到一个int三维数组中

数据将是这样的,我可以改变它的设计,以满足我的需要,因为它还没有完成。这些值都是哑的,但值得一提的是(小心mindf*ck ahead)我必须嵌套未知数量的包含整数的数组,在根节点中重复三次或更少的数组中嵌套两次

int[3 or less][2][unknown int] = val

我写这些键是为了提高可读性,它们可能是也可能不是实际json的一部分

{
    demand : {
        0 : {
           0 :{
               0 :22,
               1 :32,
               2 :21
            },
            1 :{
               0 :2762,
               1 :352,
               2 :231
            }
        },
        1 :{
            0 :{
               0 :222,
               1 :232,
               2 :621
             },
             1 :{
               0 :272,
               1 :37762,
               2 :261 
             }
         }
     }
}

关键是键和值都是整数,我想用它创建一个int [][][]。我认为答案就在这个文档中:Jackson Full Databinding但我不太明白它对我的数据是如何起作用的

我在考虑一些ObjectMapper。readValue(json,new TypeReference>;(){})`并将继续对此进行研究,但我没有太多希望

谢谢你的帮助

编辑以下是实际有效的JSON

[  [   [  22, 32,  21   ],  [ 2762,  352, 231 ] ], [    [  222, 232,  621 ],  [ 272,  37762, 261]] ]

共 (2) 个答案

  1. # 1 楼答案

    我不确定这是否回答了您的问题,但我认为您可以始终使用迭代器和拆分,例如:

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import net.sf.json.JSONObject;
    
    public class test {
        /**
     * @param args
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Map <String, Object> map1 = new HashMap<String, Object>();
        Map <String, Object> map2 = new HashMap<String, Object>();
        Map <String, Map> map3 = new HashMap<String, Map>();
        map1.put("0","22,32,21");
        map1.put("1", "2762,352,231");
        map2.put("0", "222,232,621");
        map2.put("1","272,37762,261");
        map3.put("0",map1 );
        map3.put("1",map2);
        JSONObject jsonobj = JSONObject.fromObject(map3);  // creating json object
        Iterator it = jsonobj.keys();
        int maxZ = 0; //find out the length of the last dimension
        while(it.hasNext()) {
            JSONObject jobj = (JSONObject) jsonobj.get(it.next());
            Iterator it2 = jobj.keys();
            while(it2.hasNext()) {
                if(((String)jobj.get(it2.next())).split(",").length > maxZ){
                    maxZ= ((String)jobj.get(it2.next())).split(",").length;
                    }
            }
        }
        int[][][] result= new int [jsonobj.size()][2][maxZ]; //creating 3D array of the right size
        int x = 0, y = 0, z = 0;
        it = jsonobj.keys();
        while(it.hasNext()) {
            JSONObject jobj = (JSONObject) jsonobj.get(it.next());
            Iterator it2 = jobj.keys();
            y = 0; //reset y
            while(it2.hasNext()) {
                String[]s =((String)jobj.get(it2.next())).split(",");
                z = 0; //reset z
                for (String str : s) {
                    result[x][y][z] = Integer.parseInt(str);
                    z++;
                }
            y++;
            }
        x++;
        }
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                for (int k = 0; k < z; k++) {
                    System.out.print(result[i][j][k]);
                    System.out.print(" ");
                }
                System.out.println("\n");
                }
            }
        }
    }
    

    输出:

    272 37762 261 
    
    222 232 621 
    
    2762 352 231 
    
    22 32 21 
    

    不知道为什么这是在后面的顺序,抱歉,如果这没有帮助:p

  2. # 2 楼答案

    使用Jackson序列化/反序列化数组与序列化其他任何内容相同:

    public class App 
    {
        public static void main(String[] args) throws JsonProcessingException {
    
            int[][][] a = new int[2][3][2];
            a[0][2][0] = 3;
    
            ObjectMapper om = new ObjectMapper();
    
            // Serialize to JSON
            String json = om.writeValueAsString(a);
            System.out.println(json);
    
            // deserialize back from JSON
            a = om.readValue(json, int[][][].class);
            System.out.println(a[0][2][0]);
    
       }
    }
    

    输出:

    [[[0,0],[0,0],[3,0]],[[0,0],[0,0],[0,0]]]
    3

    也就是说,除非你知道它总是一个三维数组,否则使用List会更好