0

I have this link: JSON file

This is stored on a online server. All fine but I want to get an item like "hood_id" or "damage_or_theft_car_id" but somehow my code crashes when I launch it. With an error Json object not found

This is what I tried:

public class JSONParser {
    private String read(BufferedReader bufferedReader) throws IOException {
        //Creates new StringBuilder to avoid escaping chars
        StringBuilder stringBuilder = new StringBuilder();
        //Gets the currentLine
        String currentLine;
        while((currentLine = bufferedReader.readLine()) !=null ){
            //Adds the currentLine to the stringBuild if the currentLine is not null
            stringBuilder.append(currentLine);
        }
        //Returns the StringBuilder is String format
        return stringBuilder.toString();
    }

public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
    InputStream url = new URL(JSONurl).openStream();
    try {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url));
        String jsonText = read(bufferedReader);
        JSONArray jsonArray = new JSONArray("[" +jsonText + "]");
        //JSONObject json = new JSONObject(jsonText);
        JSONObject json = jsonArray.getJSONObject(0);
        return json;
    } finally {
        url.close();
    }
}

public void printJSON() throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("http://test.dontstealmywag.ga/api/damage_or_theft_car.php");
    System.out.println(json.toString());
    System.out.println(json.get("hood_id"));
}

}

When I enter json.get("damage_or_theft_car"); The code works. Can anybody help me out? I think the issue is in JSONArrays and JSONOjects

4
  • 1
    "Somehow my code crashes" What do you mean by somehow? Do you have an error? If yes, can you add the stacktrace? Commented Jun 27, 2017 at 14:58
  • @Nathan sorry added the error. JsonObject not found Commented Jun 27, 2017 at 15:00
  • Could you please post complete error stack here? Copy and paste complete stacktrace from your IDE/browser. Commented Jun 27, 2017 at 15:04
  • @Sanderbakker At which line? Just post the stacktrace in your question, it'll be more clear ^^ Commented Jun 27, 2017 at 15:20

1 Answer 1

1

"hood_id" is not in the top hierarchy of the json, this is why you probably get :

org.json.JSONException: JSONObject["hood_id"] not found

at the line: System.out.println(json.get("hood_id"));

Just to be sure: when you run:

System.out.println(json.get("damage_or_theft_car"));

The program will execute the line and print the json content.


If you do want to print the value of hood_id, you need to dig into each json in the list. For example:

System.out.println(json.getJSONArray("damage_or_theft_car")
                          .getJSONObject(0).get("hood_id"));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works. How can I get the size of the Array?
you're welcome. to get the size you just need to use length : json.getJSONArray("damage_or_theft_car").length()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.