first of all.. If there is missing some information just ask and I will give the needed input.
I have a .json file on my Desktop, and now I have to use java and have to extract values out of the .json file. This is my task: **write a script which extract the value “automatic -> kernel -> release” out of the attached json file.
example: output:
3.16.0-5-amd64**
The json file has 27k+ lines, I don't know if this can be a problem or not.
the .json file includes this:
1 {
2 “results”: 3,
3 “rows”: [
4 {
5 “name”: “test”,
6 “chef_environment”: “office”,
7 “json_class”: “Chef::Node”,
8 “automatic”: {
9 “nginx”: {
10 “version”: null,
11 “configure_arguments”: [
12
13 ],
14 “prefix”: null,
15 “conf_path”: null
16 },
.....
98 “kernel”: {
99 “name”: “Linux”,
100 “release”: “3.16.0-5-amd64"
And this is my code now:
package json;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import java.util.Iterator;
public class json {
public static void main(String[] args) throws Exception {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("C:\\Users\\admin-elias\\Desktop\\nginx.json"));
JSONObject jsonObject = (JSONObject) obj;
String automatic = (String) jsonObject.get("automatic");
String kernel = (String) jsonObject.get("kernel");
JSONArray release = (JSONArray) jsonObject.get("release");
System.out.println("automatic: "+automatic);
System.out.println("kernel: "+kernel);
System.out.println("release: "+release);
;
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I try to run the program, I get the following error:
java.lang.NumberFormatException: For input string: "18446744073709551615"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Long.parseLong(Long.java:692)
at java.base/java.lang.Long.valueOf(Long.java:1144)
at org.json.simple.parser.Yylex.yylex(Yylex.java:660)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
at json.json.main(json.java:18)
What am I doing wrong? I am relatively new to java and I need to get this program running so every suggestion or help would be very nice. :)
Edit:
I finally got my code which works just perfectly:
public class json {
public static void main(String[] args) throws Exception {
try {
JsonReader jsonReader = new JsonReader(new FileReader("filename"));
Gson gson = new Gson();
JsonElement json = gson.fromJson(jsonReader, JsonElement.class);
//JsonElement json = new JsonParser().parse(jsonString);
JsonArray array = json.getAsJsonObject().get("rows").getAsJsonArray();
int length = array.size();
for (int i = 0; i < length; i++) {
System.out.println(array.get(i).getAsJsonObject().get("automatic").getAsJsonObject().get("kernel").getAsJsonObject().get("release").getAsString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}