JSON Data Formatting - java

I am passing the following JSON object from a .jsp page to a java servlet using JSON.stringify and JQuery.ajax():
{"bin":[{"binId":"0","binDetails":[{"productCode":"AU192","qty":"4"},{"productCode":"NE823","qty":"8"}],"comments":"store pickup"},{"binId":"1","binDetails":[{"productCode":"AF634","qty":"2"}],"comments":""},{"binId":"2","binDetails":[{"productCode":"QB187","qty":"3"}],"comments":"international shipping"},{"binId":"3","binDetails":[{"productCode":"AF634","qty":"2"},{"productCode":"QB187","qty":"2"}],"comments":""}]}
This is the code in my java servlet:
StringBuffer strBuffer = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null){
strBuffer.append(line);
}
} catch (Exception e) {
}
try {
JSONObject jsonObj = new JSONObject(new JSONTokener(strBuffer.toString()));
// I call a method here and pass jsonObj
} catch (Exception e) {
}
In the method to which I pass jsonObj I am using jsonObj.length() to find out how many items are in jsonObj and it tells me 1, which in this case I would have expected 3. I even tried this:
JSONObject bins = jsonObj.get("bin");
bins.length();
which told me jsonObj.get("bin") was not a JSONObject. Is my data formatted incorrectly before I pass it from my .jsp or am I using the JSONObject in my java servlet incorrectly? How do I access the values in the JSONObject?

JSONArray bins = jsonObj.getJSONArray("bin");
bins.length();

Related

Java : Not Able to cast jsonobject to Json array

I was able to pull data from an api using my get request method
{"vulnerabilities":[{"id":5027994,"status":"open","closed_at":null,"created_at":"2019-06-07T06:10:15Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-reader-apsb09-15-cve-2009-2990"],"last_seen_time":"2019-07-24T05:00:00.000Z","fix_id":4953,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-reader-apsb09-15-cve-2009-2990","open":true}],"asset_id":119920,"connectors":[{"name":"Nexpose Enterprise","id":7,"connector_definition_name":"Nexpose Enterprise","vendor":"R7"}],"service_ticket":null,"urls":{"asset":"dummy.com"},"patch":true,"patch_published_at":"2009-10-08T22:40:52.000Z","cve_id":"CVE-2009-2990","cve_description":"Array index error in Adobe Reader and Acrobat 9.x before 9.2, 8.x before 8.1.7, and possibly 7.x through 7.1.4 might allow attackers to execute arbitrary code via unspecified vectors.","cve_published_at":"2009-10-19T22:30:00.000Z","description":null,"solution":null,"wasc_id":null,"severity":9,"threat":9,"popular_target":false,"active_internet_breach":true,"easily_exploitable":true,"malware_exploitable":true,"predicted_exploitable":false,"custom_fields":[],"first_found_on":"2019-06-05T05:22:23Z","top_priority":true,"risk_meter_score":100,"closed":false}
but in my request method I have received an error in parsing this data , which is that I can not cast an jsonobject to jsonarray.
here is the get request method:
public static void GetRequest() {
BufferedReader reader;
String access_token = "blahblahblah";
String line;
StringBuffer responseContentReader = new StringBuffer();
try {
URL url = new URL("https://api.security.com/vulnerabilities/");
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("X-Risk-Token ", access_token);
connection.setRequestProperty("Accept", "application/json");
//here we should be able to "request" our setup
//Here will be the method I will use
connection.setRequestMethod("GET");
//after 5 sec if the connection is not successful time it out
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
//System.out.println(status); //here the connect was established output was 200 (OK)
//here we are dealing with the connection isnt succesful
if (status > 299) {
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
while ((line = reader.readLine()) != null) {
responseContentReader.append(" ");
responseContentReader.append(line);
responseContentReader.append("\n");
}
reader.close();
//returns what is successful
} else {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
responseContentReader.append(" ");
responseContentReader.append(line);
responseContentReader.append("\n");
}
reader.close();
}
JsonParser parser = new JsonParser();
Object object = parser.parse(responseContentReader.toString());
JsonArray array = (JsonArray) object; // here is where the exception occurs
for (int i = 0; i < array.size(); i++) {
JsonArray jsonArray = (JsonArray) array.get(i);
JsonObject jsonobj = (JsonObject) jsonArray.get(i);// cannot cast jsonobject to jsonarray
}
//JsonObject jsonObject = (JsonObject) object;
System.out.println( responseContentReader.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
connection.disconnect();
}
}
here is where the error occurs specifically, I honestly dont know how to go forward with this:
JsonParser parser = new JsonParser();
Object object = parser.parse(responseContentReader.toString());
JsonArray array = (JsonArray) object; // here is where the exception occurs
for (int i = 0; i < array.size(); i++) {
JsonArray jsonArray = (JsonArray) array.get(i);
JsonObject jsonobj = (JsonObject) jsonArray.get(i);// cannot cast jsonobject to jsonarray
}
//JsonObject jsonObject = (JsonObject) object;
System.out.println( responseContentReader.toString());
the error message is
Exception in thread "main" java.lang.ClassCastException: com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray
you forgot to close your json with brackets and braces like this:
{"vulnerabilities":[{"id":5027994,"status":"open","closed_at":null,"created_at":"2019-06-07T06:10:15Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-reader-apsb09-15-cve-2009-2990"],"last_seen_time":"2019-07-24T05:00:00.000Z","fix_id":4953,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-reader-apsb09-15-cve-2009-2990","open":true}],"asset_id":119920,"connectors":[{"name":"Nexpose Enterprise","id":7,"connector_definition_name":"Nexpose Enterprise","vendor":"R7"}],"service_ticket":null,"urls":{"asset":"dummy.com"},"patch":true,"patch_published_at":"2009-10-08T22:40:52.000Z","cve_id":"CVE-2009-2990","cve_description":"Array index error in Adobe Reader and Acrobat 9.x before 9.2, 8.x before 8.1.7, and possibly 7.x through 7.1.4 might allow attackers to execute arbitrary code via unspecified vectors.","cve_published_at":"2009-10-19T22:30:00.000Z","description":null,"solution":null,"wasc_id":null,"severity":9,"threat":9,"popular_target":false,"active_internet_breach":true,"easily_exploitable":true,"malware_exploitable":true,"predicted_exploitable":false,"custom_fields":[],"first_found_on":"2019-06-05T05:22:23Z","top_priority":true,"risk_meter_score":100,"closed":false}]}
try this json and it should work!
I highly recommend you use this website for formmating your jsons
https://jsonformatter.org/
Instead of casting, retrieve the array from the object or json response like below:
JsonObject object = ...
JsonArray array = object.getJSONArray("vulnerabilities");
The response returned by the GET API is not a JsonArray but a JsonObject.
You can correct the following line -- JsonArray array = (JsonArray) object; to --
JsonObject jsonObject = (JsonObject) object;
Further, you could read the "vulnerabilities" within by --
JsonArray vulnerabilitiesArray = jsonObject.getJsonArray("vulnerabilities");

JSON Object GSON into list

I want to implement my old code when I was populating JSON array into listview but my current json haven't specified array, overall json is an array...
Can somebody tell me how to transform my code to use JSON like this?
https://api-v2.hearthis.at/categories/drumandbass/?page=15&count=2
Code to use gson, my old:
protected List<Model> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = new JSONArray("arrayname");
List<Model> dataModelList = new ArrayList<>();
Gson gson = new Gson();
for(int i=0; i<parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
Model modelList = gson.fromJson(finalObject.toString(), Model.class);
dataModelList.add(modelList);
}
return dataModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
Instead of doing this:
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = new JSONArray("arrayname");
Just do this:
JSONArray parentArray = new JSONArray(finalJson);
If i understand the problem correctly, i guess you are having trouble parsing the array who's name is not specified with a key. So, a simple but not a very good way of doing this would be, when you create your finalJson like this:
String finalJson = buffer.toString();
just add a line to check if it contains an array opening bracket, like this:
if(finalJson.contains("[")){
finalJson = finalJson.replace("[","{
"arrayname": [");
}
also, close the array appropriately.
if(finalJson.contains("]")){
finalJson = finalJson.replace("]","]
}");
}
This way, u will have a name for the array to parse, and i guess this is what you are looking for. But this approach might fail if you have more than 1 array in your Json, in that case you will have to use startsWith & endsWith string methods to give a name to your array. But, as of now, from what your Json looks like, this would work.

Spring MVC can not fetch JSON value

In Spring mvc I have a mytable.json file.
I want to fetch that json file data and then want to add to model.addAttribute().
mytable.json
{"name1":["place1.1","place1.2"],
"name2":["place2.1","place1.2"]
...........
.........}
I want to fetch the names with their corresponding citylist.
Ex:
name1=place1.1,place1.2
so,I have done:--
try {
JSONParser parser = new JSONParser();
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/mytable.json").getFile());
JSONObject obj = (JSONObject) parser.parse(new FileReader(file));
Iterator<String> keys = obj.values().iterator();
while( keys.hasNext() )
{
String key = (String)keys.next();
if ( obj.get(key) instanceof JSONObject )
{
model.addAttribute("key", key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
But I am getting error:
Unexpected character (�) at position 0.
in this line :
JSONObject obj = (JSONObject) parser.parse(new FileReader(file));
why??Where is the problem?
The JSONParser parser = new JSONParser(); is expecting a JSON String, not a .json file. hence the Unexpected character.... error.
You can InputStreamReader:
jsonObject = (JSONObject) parser.parse(new InputStreamReader(new FileInputStream("file/mytable.json")));

The method getJSONObject(String) is undefined for the type JSONObject

I am returning a json from my class:
#POST("/test")
#PermitAll
public JSONObject test(Map form) {
JSONObject json=new JSONObject();
json.put("key1",1);
json.put("key2",2);
return json;
}
now I want to get this json from "getInputStream" and parse it to see if key1 exists:
String output = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder output = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
output=output.toString();
JSONObject jsonObj = new JSONObject();
jsonObj.put("output", output);
if (jsonObj.get("output") != null){
**//search for key1 in output**
System.out.println("key1 exists");
}else{
System.out.println("key1 doesnt exist");
}
reader.close();
How can I convert output to JSONObject and search for "key1"?
I tried following but I got errors after arrows:
JSONObject jObject = new JSONObject(output); ---> The constructor JSONObject(String) is undefined
JSONObject data = jObject.getJSONObject("data"); ---> The method getJSONObject(String) is undefined for the type JSONObject
String projectname = data.getString("name"); ----> The method getString(String) is undefined for the type JSONObject
JSONObject jsonObject = (JSONObject) JSONValue.parse(output);
Try this.
And then you can verify the existence of the field using:
jsonObject.has("key1");
You need to parse the object using a parser. Check out the documentation here: https://code.google.com/p/json-simple/wiki/DecodingExamples

Parsing String with JSONTokener to JSONObject OR JSONArray

I'm using the following code to handle REST responses from my server:
if (response.getEntity() != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
try {
rr.jsonObject = new JSONObject(tokener);
} catch (JSONException e) {
e.printStackTrace();
Log.d("parsing", "creating json array");
try {
rr.jsonArray = new JSONArray(tokener);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
If the response is an JSONObject, it works perfectly but if the server returns a JSONArray, the second try block also throws although it's correct json.
03-30 14:09:15.069: W/System.err(6713): org.json.JSONException: End of input at character 314 of [{"__className":"stdClass","char3code":"DEU","fips_name":"Germany","alternate_names":"Germany, Deutschland, Allemagne, Alemania"},{"__className":"stdClass","char3code":"USA","fips_name":"United States","alternate_names":"United States of America, Vereinigte Staaten von Amerika, \u00c9tats-Unis, Estados Unidos"}]
I expect the reason that this is failing is that when you call new JSONArray(tokener) the tokener is no longer positioned at the start of the token stream. Try creating a new JSONTokener instance for the 2nd attempt at parsing..

Categories

Resources