I read the whole JSON file but I need to read values from key rating and store into ArrayList,here is code but it does not do anything
ArrayList<Object> rates = new ArrayList<>();
FileReader read = new FileReader("C:\\Users\\User\\eclipse-workspace\\Task\\files\\reviews.json");
JSONParser parser = new JSONParser();
Object obj = parser.parse(read);
JSONObject jsonObject = new JSONObject();
jsonObject = (JSONObject) obj;
JSONArray reviews = new JSONArray();
reviews = (JSONArray) jsonObject.get("reviews");
Iterator<Entry> itr1 = reviews.iterator();
while (itr1.hasNext()) {
Map rate= ((Map)jsonObject.get("rating"));
Iterator<Entry> itr2 = ((JSONObject) itr1.next()).entrySet().iterator();
while (itr2.hasNext()) {
if(itr2.equals("rating")) {
rates.add(itr2);
Map.Entry pair = itr2.next();
System.out.println(rate);
}
}
}
Based from what you've provided so far, perhaps this code snippet will give you some idea:
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
// This will be what you have in your JSON file.
String content = "{\n" +
" \"reviews\": [\n" +
" {\n" +
" \"id\": 2097047,\n" +
" \"reviewId\": \"93f131be27dc1122bb7ef0048ad10e4f\",\n" +
" \"reviewFullText\": \"5 star review\",\n" +
" \"reviewText\": \"5 star review\",\n" +
" \"numLikes\": 0,\n" +
" \"numComments\": 0,\n" +
" \"numShares\": 0,\n" +
" \"rating\": 5,\n" +
" \"reviewCreatedOn\": \"2 months ago\"\n" +
" },\n" +
" {\n" +
" \"id\": 2097048,\n" +
" \"reviewId\": \"123131be27dc1122bb7ef0048ad10e4f\",\n" +
" \"reviewFullText\": \"4 star review\",\n" +
" \"reviewText\": \"4 star review\",\n" +
" \"numLikes\": 0,\n" +
" \"numComments\": 0,\n" +
" \"numShares\": 0,\n" +
" \"rating\": 4,\n" +
" \"reviewCreatedOn\": \"3 months ago\"\n" +
" }\n" +
" ]\n" +
"}";
JSONParser parser = new JSONParser();
List<Integer> rates = new ArrayList<>();
try {
JSONObject jsonObject = (JSONObject) parser.parse(content);
JSONArray jsonArray = (JSONArray) jsonObject.get("reviews");
for (Iterator<JSONObject> itr = jsonArray.iterator(); itr.hasNext();) {
JSONObject item = itr.next();
// do your stuff here for rating. I simply added it in the list right here.
rates.add(((Long) item.get("rating")).intValue());
}
} catch (ParseException e) {
// handle exception
}
Following your prompt and your example code, this code snippet can help you
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.jupiter.api.Test;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class TestQ68867649 {
@Test
void testQ68867649() throws IOException, ParseException {
ArrayList<Long> rates = new ArrayList();
FileReader read = new FileReader("C:\\Users\\User\\eclipse-workspace\\Task\\files\\reviews.json");
JSONParser parser = new JSONParser();
Object obj = parser.parse(read);
JSONObject jsonObject = (JSONObject) obj;
JSONArray reviews = (JSONArray) jsonObject.get("reviews");
for (Iterator<JSONObject> iterator = reviews.iterator(); iterator.hasNext(); ) {
JSONObject rate = iterator.next();
rates.add((Long) rate.get("rating"));
}
System.out.println("rates values: " + rates);
}
}
reviews.json file example data based on the data provided by you
{
"reviews": [
{
"id": 2097047,
"reviewId": "93f131be27dc1122bb7ef0048ad10e4f",
"reviewFullText": "5 star review",
"reviewText": "5 star review",
"numLikes": 0,
"numComments": 0,
"numShares": 0,
"rating": 5,
"reviewCreatedOn": "2 months ago"
},
{
"id": 2097047,
"reviewId": "93f131be27dc1122bb7ef0048ad10e4f",
"reviewFullText": "5 star review",
"reviewText": "5 star review",
"numLikes": 0,
"numComments": 0,
"numShares": 0,
"rating": 3,
"reviewCreatedOn": "2 months ago"
}
]
}
Output base on the data used as example rates values: [5, 3]
pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>q68867649</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
NOTE: this example was developed using Java 8 and Maven
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.