用Java压平json文档

时间:2017-05-03 09:35:20

标签: java json flatten

我是Java的新手,我正在寻找一种平整json文档的方法。

我尝试过Object mapper但没有成功。我找到了这个链接,但结果不是我需要的:https://github.com/wnameless/json-flattener

我需要转换文档,如下例所示:

以下是我的文档示例

收到的文件:

{ 
                "data1": "A", 
                "data2": "B", 
                "data3": "C", 
                "data4": [{ 
                               "subdata": [{ 
                                               "subsubdata": "mam" 
                               }, 
                               { 
                                               "subsubdata": "mom" 
                               }, 
                               { 
                                               "subsubdata": "mim" 
                               }] 
                }] 
} 

转换的文件:

{  
                "data1": "A",  
                "data2": "B",  
                "data3": "C",  
                "data4": [  
                               {"subdata": {  
                                               "subsubdata": "mam"  
                                            } 
                               },  
                               {"subdata" : {  
                                               "subsubdata": "mom"  
                                            } 
                               },  
                               {"subdata" : {  
                                               "subsubdata": "mim"  
                                            } 
                               } 
                ] 
}  

由于

1 个答案:

答案 0 :(得分:0)

try{
        JSONObject jsonObject = new JSONObject(documentReceived);
        String data1 = jsonObject.getString("data1");
        String data2 = jsonObject.getString("data2");
        String data3 = jsonObject.getString("data3");

        JSONArray jsonArray = jsonObject.getJSONArray("data4");
        List<String> listSubData = new ArrayList<String>();

        for(int i=0; i<jsonArray.length(); i++){
            JSONObject subDataObject = jsonArray.getJSONObject(i);

            JSONArray subDataArray = subDataObject.getJSONArray("subdata");
            for(int j=0; j<subDataArray.length(); j++){
                JSONObject subSubDataObject = subDataArray.getJSONObject(j);
                String subSubDataStr = subSubDataObject.getString("subsubdata");

                //adding single subsubLineItem to list
                listSubData.add(subSubDataStr);
            }
        }

        //Creating doucumentTransformed object

        jsonObject = new JSONObject();
        jsonObject.put("data1", data1);
        jsonObject.put("data2", data2);
        jsonObject.put("data3", data3);
        jsonArray = new JSONArray();
        for(int i=0; i<listSubData.size(); i++){
            JSONObject subDataObject = new JSONObject();
            JSONObject subSubDataObject = new JSONObject();
            subSubDataObject.put("subsubdata", listSubData.get(i));
            subDataObject.put("subdata", subSubDataObject);
            jsonArray.put(subDataObject);
        }

        jsonObject.put("data4", jsonArray);

        //here is your document transformed data
        System.out.println(jsonObject);
    }catch(Exception e){
        e.printStackTrace();
    }