I have a Problem in Java, specially in work with the JSON-simple Library. I have found here a code, they does work in the level of a parent node from a json-file but at my case, i read a json-file with childs under the parents.
Link to the code: How to read json file into java with simple JSON library
In DB-Language: i have a database with some tables. Now, i can only read "select * from table" but i want to read the column (or attribute) from it.
The Structure (raw data json):
{
"PARENT1":
{
"child_attr1":"0.00","child_attr2":"0.30"
},
"PARENT2":
{
"child_attr1":"0.10","child_attr2":"0.12"
},
"PARENT3":
{
"child_attr1":"0.03","child_attr2":"0.45"
}
}
The Code:
public static HttpResponse http(String url, String body) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpPost request = new HttpPost(url);
StringEntity params = new StringEntity(body);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse result = httpClient.execute(request);
String json_content = EntityUtils.toString(result.getEntity(), "UTF-8");
//System.out.println(json_content);
try {
JSONParser parser = new JSONParser();
Object resultObject = parser.parse(json_content);
if (resultObject instanceof JSONArray) {
JSONArray array=(JSONArray)resultObject;
for (Object object : array) {
JSONObject obj =(JSONObject)object;
System.out.println(obj.get("Parent"));
System.out.println(obj.get("Child"));
//System.out.println("case1");
}
} else if (resultObject instanceof JSONObject) {
JSONObject obj =(JSONObject)resultObject;
System.out.println(obj.get("PARENT2"));
//System.out.println("case2");
//THIS KNOT WORKS BUT IT GIVES ME ALL VALUES OF THE ATTRIBUTES
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}