英文:
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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论