Here is my Java Class
I am getting below Exception, My Requirement is : Read the Json entire Json file and return it, can come one please help me to figure this out
package Resources;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.fasterxml.jackson.core.JsonParser;
public class ObjectRepository {
public static JSONObject readJsonFile(String fileName) throws IOException, Exception {
fileName="C:\\Users\\AamP\\eclipse-workspace\\globalData.json";
JSONParser myParser = new JSONParser();
Object myObject = myParser.parse(new FileReader(fileName));
JSONObject myJsonObject = (JSONObject) myObject;
return myJsonObject;
}
}
java.lang.ClassCastException: class org.json.simple.JSONArray cannot be cast to class org.json.JSONObject (org.json.simple.JSONArray and org.json.JSONObject are in unnamed module of loader 'app')
at Resources.ObjectRepository.readJsonFile(ObjectRepository.java:41)
at Resources.BaseClass.initializeDriver(BaseClass.java:57)
at Nimbly.nimbly_ui.SampleTest.loadUrl(SampleTest.java:29)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:637)
at org.testng.TestRunner.beforeRun(TestRunner.java:627)
at org.testng.TestRunner.run(TestRunner.java:589)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
CodePudding user response:
Why not directly use org.json.JSONObject
JSONObject myJsonObject = new JSONObject(new FileReader(fileName));
CodePudding user response:
public static void main(String[] args) {
try {
FileReader fr = new FileReader("path/to/file.json");
JSONParser parser = new JSONParser();
JSONArray jObj = (JSONArray) parser.parse(fr);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
Use JSONObject for JSON like
{
"id":"101",
"name":"Tarun"
}
and JSONArray for array of JSON like
[
{"id":"1","name":"ankur"},
{"id":"2","name":"mahajan"}
]
you can wrap it in a func. and return the jObj according to your needs.
CodePudding user response:
A JSON Object looks like:
{"foo": "bar", ... }
while a JSON Array looks like: ["foo", "bar", ...]
.
If you read the documentation for org.json.simple
you'll see that parse
returns Object
and that org.json.JSONObject
is not part of that library.
If you want to use org.json.simple
, then your readJsonFile
method will need to return Object
, unless you are certain that the JSON file contains a JSON Array, i.e. ["foo", "bar", ...]
, in which case you can cast to org.json.simple.JSONArray
.
This will give you a ClassCastException
when your JSON file contains a JSON Object instead of an array.