Reputation:
I want to get the number of properties in the rowdata
element, in the example JSON below:
"rowData": [
{
"_idName": "CONFIG_DATA_ENTITY_MAP.CONFIG_DT_ENTITY_MAP_ID",
"ERROR": null,
"_id": 1,
"ENTITY_TYPE": "BASE_TIMESERIES",
"DATA_TYPE": "Date",
"FIX_THIS": 0,
"ENTITY_NAME": "ASP",
"SOURCE_TABLE_COLUMN": "AS_OF_DATE"
},
{
"_idName": "CONFIG_DATA_ENTITY_MAP.CONFIG_DT_ENTITY_MAP_ID",
"ERROR": null,
"_id": 2,
"ENTITY_TYPE": "BASE_TIMESERIES",
"DATA_TYPE": "String",
"FIX_THIS": 0,
"ENTITY_NAME": "ASP",
"SOURCE_TABLE_COLUMN": "CUSTOMER_ID"
}
]
Upvotes: 0
Views: 2706
Reputation:
package com.example.demo;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ColumnDefValidator {
public static void columnDef(JSONObject jsonObjects) {
int templengthColumn = 0;
int templelenghtRow = 0;
try {
JSONArray columnDefsData = jsonObjects.getJSONArray("columnDefs");
JSONArray rowDataDefs = jsonObjects.getJSONArray("rowData");
for (int i = 0; i < columnDefsData.length(); i++) {
JSONObject temp = columnDefsData.getJSONObject(i);
templengthColumn = temp.length();
}
for (int i = 0; i < rowDataDefs.length(); i++) {
JSONObject temp = rowDataDefs.getJSONObject(i);
templelenghtRow = temp.length();
}
System.out.println(templengthColumn);
System.out.println(templelenghtRow);
} catch (JSONException err) {
System.out.println(err.toString());
}
}
public static JSONObject parseJSONFile(String filename) throws JSONException, IOException {
String content = new String(Files.readAllBytes(Paths.get(filename)));
return new JSONObject(content);
}
public static void main(String[] args) throws IOException, JSONException, ParseException {
String filename = "/home/niteshb/WaveProject/working_dir/jsonoutput/aggrid/resp_attribute_report.json";
JSONObject jsonObject = parseJSONFile(filename);
columnDef(jsonObject);
}
}
Upvotes: 0
Reputation: 302
Have you tried converting into JSONObject
and use jsonObject.length()
?
Here's the rough code:
public static void main(String[] args) {
String jsonData = "{\"rowData\": [\n" +
" {\n" +
" \"_idName\": \"CONFIG_DATA_ENTITY_MAP.CONFIG_DT_ENTITY_MAP_ID\",\n" +
" \"ERROR\": null,\n" +
" \"_id\": 1,\n" +
" \"ENTITY_TYPE\": \"BASE_TIMESERIES\",\n" +
" \"DATA_TYPE\": \"Date\",\n" +
" \"FIX_THIS\": 0,\n" +
" \"ENTITY_NAME\": \"ASP\",\n" +
" \"SOURCE_TABLE_COLUMN\": \"AS_OF_DATE\"\n" +
" },\n" +
" {\n" +
" \"_idName\": \"CONFIG_DATA_ENTITY_MAP.CONFIG_DT_ENTITY_MAP_ID\",\n" +
" \"ERROR\": null,\n" +
" \"_id\": 2,\n" +
" \"ENTITY_TYPE\": \"BASE_TIMESERIES\",\n" +
" \"DATA_TYPE\": \"String\",\n" +
" \"FIX_THIS\": 0,\n" +
" \"ENTITY_NAME\": \"ASP\",\n" +
" \"SOURCE_TABLE_COLUMN\": \"CUSTOMER_ID\"\n" +
" }\n" +
"]}";
try {
JSONObject jsonObject = new JSONObject(jsonData.replace("\n", ""));
JSONArray jsonArray = jsonObject.getJSONArray("rowData");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject temp = jsonArray.getJSONObject(i);
System.out.println("temp = " + temp.length());
}
} catch (JSONException err){
Log.d("Error", err.toString());
}
}
Upvotes: 2
Reputation: 54
A raw solution without exception handling. I use the library 'org.json' and had to wrap your raw String inside braces to make it json compatible:
final String jsonString = "{\"rowData\": [\n"
+ " {\n"
+ " \"_idName\": \"CONFIG_DATA_ENTITY_MAP.CONFIG_DT_ENTITY_MAP_ID\",\n"
+ " \"ERROR\": null,\n"
+ " \"_id\": 1,\n"
+ " \"ENTITY_TYPE\": \"BASE_TIMESERIES\",\n"
+ " \"DATA_TYPE\": \"Date\",\n"
+ " \"FIX_THIS\": 0,\n"
+ " \"ENTITY_NAME\": \"ASP\",\n"
+ " \"SOURCE_TABLE_COLUMN\": \"AS_OF_DATE\"\n"
+ " },\n"
+ " {\n"
+ " \"_idName\": \"CONFIG_DATA_ENTITY_MAP.CONFIG_DT_ENTITY_MAP_ID\",\n"
+ " \"ERROR\": null,\n"
+ " \"_id\": 2,\n"
+ " \"ENTITY_TYPE\": \"BASE_TIMESERIES\",\n"
+ " \"DATA_TYPE\": \"String\",\n"
+ " \"FIX_THIS\": 0,\n"
+ " \"ENTITY_NAME\": \"ASP\",\n"
+ " \"SOURCE_TABLE_COLUMN\": \"CUSTOMER_ID\"\n"
+ " }\n"
+ "]}";
public int numberOfChildElement(String data, int elementIndex){
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("rowData");
return jsonArray.getJSONObject(elementIndex).length();
}
So if you call the method 'numberOfChildElement' passing in the raw String and the index of the element you want to get the count of (0 or 1 in your example), you should get the result you want.
Upvotes: 0
Reputation: 703
Convert you JSON to Object. Pass it to your controller and read the size of it
@RequestBody Map<String, Object> objectMap
objectMap.size()
Upvotes: 0