JSON:嵌套数组

时间:2018-08-21 03:59:04

标签: java json nested

我有一个程序可以从数据库(JSON MySQL中的数据存储)中检索数据。

public static int selectData(Connection conn, String db_type) throws SQLException {

     JSONObject obj = new JSONObject();

    String q = "SELECT * FROM common_attr_test";
        PreparedStatement preparedStatement = conn.prepareStatement(q);
        preparedStatement.execute();
        ResultSet rs = preparedStatement.executeQuery();
        while (rs.next()) {
                String uuid_user = rs.getString("uuid");
                String attributes_uuid = rs.getString("attributes");

                obj.put("uuid", uuid_user);
                obj.put("attributes",  attributes_uuid);  
        }

            System.out.println("JSON Obj: "+obj);

    return 1;
 } // end selectData function

我设法得到了物体。输出为:

  

JSON对象:

 {
  "attributes": "{\"1\": {\"1\": 2, \"2\": 2, \"3\": 3}, \"2\": {\"h4y4/1123\": 4, \"h4yp:/4/1123\": 1, \"h4yyp:/4/1123\": 1, \"httyyyyp:/4/1123\": 1}, \"3\": {\"Chrome|Windows NT 6.1\": 7}, \"7\": {\"2\": 4, \"6\": 1}, \"8\": {\"1\": 1, \"2\": 1, \"3\": 1, \"4\": 1, \"5\": 1, \"6\": 1, \"7\": 1}}",
  "uuid": "izyani1234561"
}

我需要有关如何处理数据并将信息放入不同数组/对象的建议。例如

    array1 - 1:{1:2, 2:2, 3:3}
    array2 - 7:{2:4, 6:1}
    array3 - 8:{1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1}

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用Jackson Api来实现。
您必须创建与json对象相同的Pojo类(该类应具有“ attributes”,“ uuid”之类的成员)。
这是您必须使用的类

with recursive
  -- Constant table for demonstration purposes only; remove this and replace below references to "objects" with table name
  objects(id, object) as (values
    (101, '{"children":[{"children":[{"variants":{"aa":11}},{"variants":{"ab":12}}],"variants":{"a":1}},{"variants":{"b":2}}]}'::jsonb),
    (102, '{"children":[{"children":[{"variants":{"cc":33,"cd":34,"ce":35}},{"variants":{"f":36}}],"variants":{"c":3,"d":4,"e":5}},{"variants":{"f":6}}]}'::jsonb)
  ),
  tree_nodes as ( -- Flatten the tree by walking all 'children' and creating a separate record for each root
    -- non-recursive term: get root element
    select
      o.id, o.object as value
    from
      objects o
    union all
    -- recursive term - get JSON object node for each child
    select
      n.id,
      e.value
    from
      tree_nodes n,
      jsonb_array_elements(n.value->'children') e
    where
      jsonb_typeof(n.value->'children') = 'array'
  ),
  variants as (
    select
      n.id,
      v.value
    from
      tree_nodes n,
      jsonb_each(n.value->'variants') v -- expand variants
    where
      jsonb_typeof(n.value->'variants') = 'object'
  )
select
  id,
  jsonb_agg(value)
from
  variants
group by
  id
;

和代码

com.fasterxml.jackson.core.JsonFactory;
com.fasterxml.jackson.core.JsonParser;
com.fasterxml.jackson.databind.ObjectMapper;

现在,您可以使用Mapperclass的getter方法来获取java对象或Arrays等中的json属性

ObjectMapper objMapper=new ObjectMapper();
JsonFactory jfactory = new JsonFactory();
JsonParser jParser=jfactory.createJsonParser(jsonString); //json Object as String

Mapperclass mapper=objMapper.readValue(jParser,Mapperclass.class);// Mapperclass is Pojo for your jsonObject