java – 将 JSON 字符串在 Java 中拆分为多个 JSON 字符串

huangapple go评论80阅读模式
英文:

java - Splitting JSON string into JSON strings in Java

问题

{
    "source": {
        "sourceId": 388
    },
    "columns": [
        {
            "id": null,
            "function": "SUM",
            "operationErrorCode": 0,
            "totalAllUsingFunction": "Auto"
        },
        {
            "id": null,
            "function": "SUM",
            "operationErrorCode": 0,
            "totalAllUsingFunction": "Auto"
        }
    ],
    "columnSummaryType": 0,
    "drilldownDefaultDimension": {}
}
英文:

I have below json string

{
   "folderId":1,
   "parentIds":{},
   "folderName":null,
   "hiddenFlag":true,
   "autoExecute":false,
   "updatedDate":"2020-10-15T11:40:55.000Z",
   "widgetItems":[
      {
         "width":4,
         "property":{
            "name":"GRID",
            "widgetdef":{
               "id":"91209e5a-6468-4912-b35a-ebfe84ac867e",
               "name":"GRID",
               "Title":{
                  "type":"TEXT",
                  "value":""
               },
               "dimCount":0,
               "reportTO":{
                  "source":{
                     "sourceId":388
                  },
                  "columns":[
                     {
                        "id":null,
                        "function":"SUM",
                        "operationErrorCode":0,
                        "totalAllUsingFunction":"Auto"
                     },
                     {
                        "id":null,
                        "function":"SUM",
                        "operationErrorCode":0,
                        "totalAllUsingFunction":"Auto"
                     }
                  ],
                  "columnSummaryType":0,
                  "drilldownDefaultDimension":{
                     
                  }
               },
               "splitBar":{
                  "value":true
               },
               "isExistingDashboard":true,
               "dimensionColumnCount":0
            }
         }
      }
   ],
   "dashboardName":"Second Dashboard",
   "dashboardType":1,
   "parentFolderId":1,
   "dashboardId":90208
}

Am expecting output below json after split..I want to split reportTO object

{
    "source": {
        "sourceId": 388
    },
    "columns": [{
            "id": null,
            "function": "SUM",
            "operationErrorCode": 0,
            "totalAllUsingFunction": "Auto"
        },
        {
            "id": null,
            "function": "SUM",
            "operationErrorCode": 0,
            "totalAllUsingFunction": "Auto"
        }
    ],
    "columnSummaryType": 0,
    "drilldownDefaultDimension": {}
}

答案1

得分: 3

public static void main(String[] args) throws IOException {
    String rootJson = " {\r\n" +
            "  \"folderId\": 1,\r\n" +
            "  \"parentIds\": {},\r\n" +
            "  \"folderName\": null,\r\n" +
            "  \"hiddenFlag\": true,\r\n" +
            "  \"autoExecute\": false,\r\n" +
            "  \"updatedDate\": \"2020-10-15T11:40:55.000Z\",\r\n" +
            "  \"widgetItems\": [\r\n" +
            "    { \r\n" +
            "      \"width\": 4,\r\n" +
            "      \"property\": {\r\n" +
            "        \"name\": \"GRID\",\r\n" +
            "        \"widgetdef\": {\r\n" +
            "          \"id\": \"91209e5a-6468-4912-b35a-ebfe84ac867e\",\r\n" +
            "          \"name\": \"GRID\",\r\n" +
            "          \"Title\": {\r\n" +
            "            \"type\": \"TEXT\",\r\n" +
            "            \"value\": \"\"\r\n" +
            "          }, \r\n" +
            "          \"dimCount\": 0,\r\n" +
            "          \"reportTO\": {\r\n" +
            "            \"source\": {\r\n" +
            "              \"sourceId\": 388\r\n" +
            "            },\r\n" +
            "            \"columns\": [\r\n" +
            "              {\r\n" +
            "                \"id\": null, \r\n" +
            "                \"function\": \"SUM\", \r\n" +
            "                \"operationErrorCode\": 0,\r\n" +
            "                \"totalAllUsingFunction\": \"Auto\"\r\n" +
            "              },\r\n" +
            "              {\r\n" +
            "                \"id\": null, \r\n" +
            "                \"function\": \"SUM\", \r\n" +
            "                \"operationErrorCode\": 0,\r\n" +
            "                \"totalAllUsingFunction\": \"Auto\"\r\n" +
            "              } \r\n" +
            "            ],  \r\n" +
            "            \"columnSummaryType\": 0,\r\n" +
            "            \"drilldownDefaultDimension\": {}\r\n" +
            "          },\r\n" +
            "          \"splitBar\": {\r\n" +
            "            \"value\": true\r\n" +
            "          },  \r\n" +
            "          \"isExistingDashboard\": true,\r\n" +
            "          \"dimensionColumnCount\": 0\r\n" +
            "        }\r\n" +
            "      }\r\n" +
            "    } \r\n" +
            "  ],\r\n" +
            "  \"dashboardName\": \"Second Dashboard\",\r\n" +
            "  \"dashboardType\": 1,\r\n" +
            "  \"parentFolderId\": 1,  \r\n" +
            "  \"dashboardId\": 90208\r\n" +
            "}";

    JSONObject jsonObject = new JSONObject(rootJson);
    JSONObject reportJson = jsonObject.getJSONArray("widgetItems").getJSONObject(0)
                                        .getJSONObject("property")
                                        .getJSONObject("widgetdef")
                                        .getJSONObject("reportTO");
    System.out.println(reportJson);
}
英文:
public static void main(String[] args) throws IOException  {
String rootJson = " {\r\n" + 
"  \"folderId\": 1,\r\n" + 
"  \"parentIds\": {},\r\n" + 
"  \"folderName\": null,\r\n" + 
"  \"hiddenFlag\": true,\r\n" + 
"  \"autoExecute\": false,\r\n" + 
"  \"updatedDate\": \"2020-10-15T11:40:55.000Z\",\r\n" + 
"  \"widgetItems\": [\r\n" + 
"    { \r\n" + 
"      \"width\": 4,\r\n" + 
"      \"property\": {\r\n" + 
"        \"name\": \"GRID\",\r\n" + 
"        \"widgetdef\": {\r\n" + 
"          \"id\": \"91209e5a-6468-4912-b35a-ebfe84ac867e\",\r\n" + 
"          \"name\": \"GRID\",\r\n" + 
"          \"Title\": {\r\n" + 
"            \"type\": \"TEXT\",\r\n" + 
"            \"value\": \"\"\r\n" + 
"          }, \r\n" + 
"          \"dimCount\": 0,\r\n" + 
"          \"reportTO\": {\r\n" + 
"            \"source\": {\r\n" + 
"              \"sourceId\": 388\r\n" + 
"            },\r\n" + 
"            \"columns\": [\r\n" + 
"              {\r\n" + 
"                \"id\": null, \r\n" + 
"                \"function\": \"SUM\", \r\n" + 
"                \"operationErrorCode\": 0,\r\n" + 
"                \"totalAllUsingFunction\": \"Auto\"\r\n" + 
"              },\r\n" + 
"              {\r\n" + 
"                \"id\": null, \r\n" + 
"                \"function\": \"SUM\", \r\n" + 
"                \"operationErrorCode\": 0,\r\n" + 
"                \"totalAllUsingFunction\": \"Auto\"\r\n" + 
"              } \r\n" + 
"            ],  \r\n" + 
"            \"columnSummaryType\": 0,\r\n" + 
"            \"drilldownDefaultDimension\": {}\r\n" + 
"          },\r\n" + 
"          \"splitBar\": {\r\n" + 
"            \"value\": true\r\n" + 
"          },  \r\n" + 
"          \"isExistingDashboard\": true,\r\n" + 
"          \"dimensionColumnCount\": 0\r\n" + 
"        }\r\n" + 
"      }\r\n" + 
"    } \r\n" + 
"  ],\r\n" + 
"  \"dashboardName\": \"Second Dashboard\",\r\n" + 
"  \"dashboardType\": 1,\r\n" + 
"  \"parentFolderId\": 1,  \r\n" + 
"  \"dashboardId\": 90208\r\n" + 
"}";
JSONObject jsonObject = new JSONObject(rootJson);
JSONObject reportJson = jsonObject.getJSONArray("widgetItems").getJSONObject(0).
getJSONObject("property").
getJSONObject("widgetdef").
getJSONObject("reportTO");
System.out.println(reportJson);
}

OUTPUT

{
"drilldownDefaultDimension":{
},
"columns":[
{
"function":"SUM",
"totalAllUsingFunction":"Auto",
"id":null,
"operationErrorCode":0
},
{
"function":"SUM",
"totalAllUsingFunction":"Auto",
"id":null,
"operationErrorCode":0
}
],
"columnSummaryType":0,
"source":{
"sourceId":388
}
}

huangapple
  • 本文由 发表于 2020年10月27日 14:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64549327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定