- Java Data Science Cookbook
- Rushdi Shams
- 458字
- 2021-07-09 18:44:27
Reading JSON files using JSON.simple
In this recipe, we will see how we can read or parse a JSON file. As our sample input file, we will be using the JSON file we created in the previous recipe.
Getting ready
In order to perform this recipe, we will require the following:
- Use the previous recipe to create a JSON file with book, author, and reviewer comments information. This file will be used as an input for parsing/reading in this recipe.
How to do it ...
- As we will be reading or parsing a JSON file, first, we will be creating a JSON parser:
JSONParser parser = new JSONParser();
- Then, in a
try
block, we will be retrieving the values in the fields book and author. However, to do that, we first use theparse()
method of the parser to read the input JSON file. Theparse()
method returns the content of the file as an object. Therefore, we will need anObject
variable to hold the content. Then, theobject
will be assigned to a JSON object for further processing. Notice the type cast of theObject
variable during the assignment:try { Object obj = parser.parse(new FileReader("c:test.json")); JSONObject jsonObject = (JSONObject) obj; String name = (String) jsonObject.get("book"); System.out.println(name); String author = (String) jsonObject.get("author"); System.out.println(author); }
- The next field to retrieve from the input JSON file is the review field, which is an array. We iterate over this field as follows:
JSONArray reviews = (JSONArray) jsonObject.get("messages"); Iterator<String> iterator = reviews.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
- Finally, we create catch blocks to handle three types of exceptions that may occur during the parsing, and then close the method:
} catch (FileNotFoundException e) { //Your exception handling here } catch (IOException e) { //Your exception handling here } catch (ParseException e) { //Your exception handling here } }
The entire class, the method described in this recipe, and the driver method to run the method are as follows:
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JsonReading { public static void main(String[] args){ JsonReading jsonReading = new JsonReading(); jsonReading.readJson("C:/testJSON.json"); } public void readJson(String inFileName) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader(inFileName)); JSONObject jsonObject = (JSONObject) obj; String name = (String) jsonObject.get("book"); System.out.println(name); String author = (String) jsonObject.get("author"); System.out.println(author); JSONArray reviews = (JSONArray) jsonObject.get("messages"); Iterator<String> iterator = reviews.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } catch (FileNotFoundException e) { //Your exception handling here } catch (IOException e) { //Your exception handling here } catch (ParseException e) { //Your exception handling here } } }
On successful execution of the code, you will be able to see the contents of the input file on the standard output.
推薦閱讀
- 數(shù)據(jù)挖掘原理與實(shí)踐
- 云計(jì)算環(huán)境下的信息資源集成與服務(wù)
- 文本數(shù)據(jù)挖掘:基于R語(yǔ)言
- 醫(yī)療大數(shù)據(jù)挖掘與可視化
- Access 2016數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用
- iOS and OS X Network Programming Cookbook
- 區(qū)塊鏈:看得見的信任
- 數(shù)據(jù)架構(gòu)與商業(yè)智能
- OracleDBA實(shí)戰(zhàn)攻略:運(yùn)維管理、診斷優(yōu)化、高可用與最佳實(shí)踐
- ZeroMQ
- 數(shù)據(jù)中心數(shù)字孿生應(yīng)用實(shí)踐
- 高維數(shù)據(jù)分析預(yù)處理技術(shù)
- 達(dá)夢(mèng)數(shù)據(jù)庫(kù)運(yùn)維實(shí)戰(zhàn)
- Google Cloud Platform for Developers
- 商業(yè)智能工具應(yīng)用與數(shù)據(jù)可視化