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");
I wrote a code that parses Json Data from my WordPress website and put it in a ListView, everything works perfectly, except the fact that it decided to load only 10 Json objects out of nearly 50 that exists in the URL.
After going through over and over my code, and after I've tried everything, I am going to ask here what could be the problem.
This is my class, it is called "getJsondata"
public ArrayList<GamesLibrary> getJsondata(String strurl)
{
ArrayList<GamesLibrary>arrayList=new ArrayList<GamesLibrary>();
String line="";
String res="";
InputStream in=null;
try
{
HttpURLConnection urlConnection=null;
URL url = null;
try
{
URL myURL = new URL(strurl);
URLConnection ucon = myURL.openConnection();
in = ucon.getInputStream();
Log.d("Negev", in.toString());
} catch (Exception e)
{
Log.d("asaf",e.getMessage());
}
BufferedReader br =new BufferedReader(new InputStreamReader(in,"iso-8859-1"));
StringBuffer sb=new StringBuffer("");
StringBuilder b = new StringBuilder();
String input;
while((input=br .readLine())!=null)
{
b.append(input+"\n");
}
in.close();
br.close();
try
{
JSONArray jArray = new JSONArray(b.toString());
for(int i=0;i<jArray.length();i++)
{
Log.d("asaf","try json"+i);
JSONObject json_data = jArray.getJSONObject(i);
String title = json_data.getString("title");
String content = json_data.getString("content");
String content2 = content.replace("\\n", "");
String content3 = Html.fromHtml(content2).toString();
String content4 = content3.replace("\",\"protected\":false}", "");
String title2 = title.replace("{\"rendered\"", "");
title2 = title2.replace("\"}", "");
title2 = title2.replace("\"", "");
title2 = title2.replace(":", "");
title2 = title2.replace("Date", "");
String id = json_data.getString("id");
String slug = json_data.getString("slug");
GamesLibrary gamesLibrary= new GamesLibrary(Integer.valueOf(id),title2,content4,slug);
arrayList.add(gamesLibrary);
Log.d("ff",content3 );
}
}
catch(JSONException e)
{
}
return arrayList;
}
catch (Exception e) {
// TODO: handle exception
}
return null;
}
The code works perfectly, but loads only 10 posts, why is that do you think?
Thank you!
Update:
The arraylist contains only 10 objects, the problem is that it should contain nearly 50...
Update 2: exact Json:
https://docs.google.com/document/d/1wkuAFZWn1jF-_7AO_zvrI4mo1V6paUODvaUW8TAn03k/edit?usp=sharing
I looked at your json, and it contains only 10 items :)
ids: 921, 919, 474 472 470 468 466 464 462 460
public static void main(String[] args) throws IOException {
try {
JSONObject json = new JSONObject(readUrl("http://api.wunderground.com" +"/api/106c4dee47162999/history_20060405/q/CA/San_Francisco.json"));
JSONObject data = json.getJSONObject("observations.tempm");
System.out.println(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
private static String readUrl(String string) throws IOException {
BufferedReader reader = null;
try {
String urlString = string;
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
}
2.errors :
org.json.JSONException: JSONObject["observations.tempm"] not found.
at org.json.JSONObject.get(JSONObject.java:454)
at org.json.JSONObject.getJSONObject(JSONObject.java:553)
at com.parser.ParserObject.main(ParserObject.java:17)
The intersting part of the JSON you use is:
{'history':
{'observations':
[
{'tempm':'10.0'}
]
}
}
Use this:
JSONObject json = ...;
JSONObject history = (JSONObject) json.get("history");
JSONArray observations = (JSONArry) json.get("observations");
JSONObject observation0 = (JSONObject) observations.get(0);
String tempm = observation0.get("tempm");
If you are interested in other array elements, use a different index.
'observations' isnt in root. you need to get 'history' first. 'observations' is a part of 'history'. and 'observations' is a list/array.
i suggest using 'jackson' or 'gson' library.parsing native json is really a pain.
you can even go to a extent of un/marshalling only required fields/objects from JSON.
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..
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();