I am using net.sf.json library for json To java and vice-versa conversion
i have a following string:
String jsonStr = "{\"name\" : \"abc\",\"address\" : \"def\"}"; // line 1
I tried to verify above string as valid json array using below code :
JSONArray arr = JSONArray.fromObject(jsonStr); // line 2
System.out.println(arr.isArray()); // line 3
But i get the following exception at line 2
Exception in thread "main" net.sf.json.JSONException: A JSONArray text must start with '[' at character 1 of {"name" : "abc","address" : "def"}
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
at net.sf.json.JSONArray._fromJSONTokener(JSONArray.java:1146)
at net.sf.json.JSONArray._fromString(JSONArray.java:1226)
at net.sf.json.JSONArray.fromObject(JSONArray.java:151)
at net.sf.json.JSONArray.fromObject(JSONArray.java:129)
at com.example.WsClient.main(WsClient.java:2) //
Now there is no static method in JSONArray class to verify it as valid json string, so i have to create a json array and then verify it. But i am getting the exception while creating the array.
Then how can i use this method ?
How can i verify that above string is a valid json Array ?
EDIT : Currently the string is an object but if i create a method with String as argument to convert to a jsonArray. How can i verify the string as valid json array?