Java JsonParser-class And Method Code Example
Here's an example of how to use the JsonParser class from the Apache Commons IO library to parse a JSON file and extract data from it:
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.StringBuilderWriter;
import org.json.JSONObject;
import org.json.JSONTokener;
String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONTokener tokener = new JSONTokener(jsonString);
JSONObject json = new JSONObject(tokener);
String name = json.getString("name");
int age = json.getInt("age");
String city = json.getString("city");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
In this example, a JSON string is defined, and then it is passed to a JSONTokener object which is then passed to a JSONObject, which allows the values of the JSON string to be accessed using the getString() and getInt() methods.