public class JsonReader {
public static Object[][] getData(String jsonPath, String dataObject, int totalDataRow, int totalColumnEntry)
throws Exception {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObj = jsonParser.parse(new FileReader(jsonPath)).getAsJsonObject();
JsonArray array = (JsonArray) jsonObj.get(dataObject);
return searchJsonElement(array, totalDataRow, totalColumnEntry);
}
public static Object[][] toArray(List<List<Object>> list) {
Object[][] r = new Object[list.size() + 1][];
int i = 0;
for (List<Object> next : list) {
r[i++] = next.toArray(new Object[list.size() + 1]);
}
return r;
}
public static Object[][] searchJsonElement(JsonArray jsonArray, int totalDataRow, int totalColumnEntry)
throws Exception {
Object[][] matrix = new Object[totalDataRow][totalColumnEntry];
int i = 0;
int j = 0;
for (JsonElement jsonElement : jsonArray) {
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
matrix[i][j] = entry.getValue().toString().replace("\"", "");
j++;
}
i++;
j = 0;
}
return matrix;
}
public static void main(String[] args) throws Exception {
String Json_path = "D:\\xxx\\xxx\\Login.json";
Object[][] data = JsonReader.getData(Json_path, "LoginData", 2, 3);
System.out.println(Arrays.deepToString(data));
}
}
Sunday, August 1, 2021
Json dataset to DataProvider
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment