I've a JSON file with:
"items": [
{
(...)
"volumeInfo": {
(...)
"readingModes": {
"text": true,
"image": true
},
(...)
}
How do I access 'image' and 'text' in "readingModes"? I've tried both
JSONArray readingModes = volumeInfo.optJSONArray("readingModes");
which worked for data with squared bracket, but in this case returns nulls
and
JSONObject readingModes = gobj.getJSONObject("readingModes");
which returns error - JSONObject["readingModes"] not found, where gobj is accessed:
JSONObject jobj = new JSONObject(jsonData);
JSONArray items = jobj.getJSONArray("items");
Iterator i = items.iterator();
while(i.hasNext())
{
JSONObject gobj = (JSONObject) i.next();
(...)
}
JSONObject jobj = new JSONObject(jsonData);
JSONArray items = jobj.getJSONArray("items");
Iterator i = items.iterator();
while(i.hasNext())
{
JSONObject gobj = (JSONObject) i.next();
JSONObject volumeInfo = gobj.getJSONObject('volumeInfo');
JSONObject readingModes = volumeInfo.getJSONObject('readingModes');
(...)
}
According to your JSON you need to do this.
Since readingModes is in volumeInfo, you first need to get JSON object of volumeInfo in order to access readingModes.
Related
I have an service that returns me an json object like the below
{
"header": {},
"title": {},
"terms": {
"data": {
"list": [
"string": 1,
"string1": 2,
"string2": 3
]
}
}
}
Now I need to get the keys of the list json array into a list. I have got the array into an object
List<String> allTerms = new ArrayList<String>();
String response = HttpRequest.get("http://myservice/get").body();
JSONParser parser = new JSONParser();
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
JSONObject fieldObj = (JSONObject)jsonObject.get("terms");
JSONObject queryObj = (JSONObject)fieldObj.get("data");
JSONArray termsArr = (JSONArray) queryObj.get("list");
//iterate the termsarr and get the string,string1,string2 keys alone to allTerms list
Is there a more better way to do this? Im using json-simple and a custom http client
By using basic for loop
for(int i=0; i<termsArr.length(); i++) {
String[] arr = termsArr.getString(i).split("\"");
allTerms.add(arr[1]);
}
I have JSON data and want to extract data of only specific fields in Java and store it in String.
Example,
From issues, key:651, From project name:test, updated, created This details for all records of array issues.
Sample JSON Data:
"issues": [
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"key": "651",
"fields": {
"project": {
"name": "test",
"Urls": {
"48x48": "https://test1.com",
"24x24": "https://test2.com"
},
},
"updated": "2019-03-05T13:24:56.000-0800",
"created": "2019-03-05T13:24:56.000-0800",
"status": {
"description" : "";
"name": "",
}
}
},
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"key": "321",
"fields": {
"project": {
"name": "test2",
"Urls": {
"48x48": "https://test1.com",
"24x24": "https://test2.com"
},
},
"updated": "2019-03-05T13:24:56.000-0800",
"created": "2019-03-05T13:24:56.000-0800",
"status": {
"description" : "";
"name": "",
}
}
}
]
Java code that I have tried so far jar used - (gson-2.8.5)
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
JsonObject issues0 = (JsonObject) issues.get(0);
JsonObject issues0data = (JsonObject) issues0.get("key");
String issue_key = issues0data.get("issue_key").getAsString();
System.out.println("Value of key is -> " + issue_key); // java.lang.ClassCastException: com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject
Updated Code
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues_data = (JsonArray) object.get("issues");
for(int i=0; i<issues_data.size(); i++)
{
JsonObject issues = (JsonObject) issues_data.get(i);
String issues_key = (String) issues.get("key").toString();
String project_name = (String) issues.get("name").toString(); // returns null
}
You can convert to string when you get the value.
Change your code like this see if it helps.
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
JsonObject issues0 = (JsonObject) issues.get(0);
String issue_key = (String) issues0.get("key");//<---here
System.out.println("Value of key is -> " + issue_key);
Update
If you want all values just put it inside "for":
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
for(int i=0; i<issues.size(); i++){
JsonObject issue = (JsonObject) issues.get(i);
String issue_key = (String) issue.get("key");
System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);
}
Update 2
The data "updated" and "created" are not inside "issues" they are inside "fields" to get access to them you need to get them from "fields". You have to go inside level by level to get access to variables:
JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
JsonArray issues = (JsonArray) object.get("issues");
for(int i=0; i<issues.size(); i++){
JsonObject issue = (JsonObject) issues.get(i);
String issue_key = (String) issue.get("key");
JsonObject fields = (JsonObject) issues.get("fields");
JsonObject project = (JsonObject) issues.get("project");
String project_name = (String) project.get("key");
String fields_updated = (String) fields.get("updated");
String fields_created = (String) fields.get("created");
System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);
}
You are getting an error because you are casting a JsonPrimitive to JsonObject. So instead of using
JsonObject issues0data = (JsonObject) issues0.get("key");
you should do
String issues0data = issues0.get("key").getAsString();
System.out.println("Value of key is -> " + issues0data);
Here, calling getAsString() will invoke JsonPrimitive.getAsString() method. This will take care if the primitive is boolean/number/string and convert it to string.
{
"type":
"coll",
"locations":[
{"geometry":
{"Coords":
[54.7,46.7]
}
},
{"geometry":
{"Coords":
[54.7,46.7]
}
},
{"geometry":
{"Coords":
[54.7,46.6]
}
},
{"geometry":
{"Coords":
[54.64999833333333,46.6]
}
}
]
}
In java using json.simple,here the code:
Here i assume the json data is present in file.json
Object obj = parser.parse(new FileReader( "file.json" ));
JSONObject jsonObject = (JSONObject) obj;
JSONArray loc= (JSONArray) jsonObject.get("locations");
Iterator i = loc.iterator();
while (i.hasNext()) {
JSONObject geo = (JSONObject) (i.next());
JSONObject coords = (JSONObject)geo.get("geometry");
String coord= (String)coords.get("Coords");
System.out.println(coord);
}
I have this JSON:
{
"id": 2643743,
"dt": 1485789600,
"clouds": {
"all": 90
}
}
In this JSON, how can I check for a key that has a nested JSON? For example, how can I check if clouds have nested JSON?
JSONObject json = readJsonFromUrl("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&APPID=916c290adb437e1cc53eab01798225ed");
Iterator keys = json.keys();
Iterator inskeys = json.keys();
String keyStr = null;
while(keys.hasNext()) {
keyStr = (String) keys.next();
Object keyvalue = json.get(keyStr);
Object keyType = keyvalue.getClass().getSimpleName();
...
Here is simple example:
String JSON = "{\"id\":2643743,\"dt\":1485789600,\"clouds\":{\"all\":90}}";
JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject("clouds");
I have the following Json and I want to parse the array (cars) ,
[
{
"name": "John",
"city": "Berlin",
"cars": [
"audi",
"bmw"
],
when i tried with the following code i got error
JSONParser parser = new JSONParser();
JSONArray a = (JSONArray) parser.parse(new FileReader(
"C:\\General\\Json\\json.txt"));
for (Object o : a) {
JSONObject person = (JSONObject) o;
String name = (String) person.get("name");
System.out.println(name);
String city = (String) person.get("city");
System.out.println(city);
String job = (String) person.get("job");
System.out.println(job);
}
here is the error "jsonObject cannot be resolved"
how should i overcome it?
JSONArray cars = (JSONArray) jsonObject.get("cars");
you did not declared jsonObject
JSONArray cars = (JSONArray) person.get("cars"); try this instead of JSONArray cars = (JSONArray) jsonObject.get("cars"); this PSR also correct