简体   繁体   中英

Read Json file in JAVA with io.vertx.core.json library

I want to read .json file in java and typecast it to JsonObject.Please suggest the code with Json but not JSON.I am using io.vertx.core.json.JsonObject library .

Object obj = parser.parse(new FileReader()); //this is from library simple.ore.JSON.
JsonObject obj1;
obj1 = (JsonObject)(obj);

I tried to use JSONparser for file reader which gives JSONObject but i need JsonObject.

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to io.vertx.core.json.JsonObject.

The problem is that you're using the parser from another library and expecting to get an instance of io.vertx.core.json.JsonObject . Instead, read your file containing your JSON text into a Java string. Note that you can do this using the IOUtils.toString(Reader) method. Then, use the JsonObject 's constructor. For example, you could just use something similar to the following code:

String jsonStr = IOUtils.toString(new FileReader(myFileName));
JsonObject jsonObj = new JsonObject(jsonStr);

Hope that helps!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM