0

Pls help me to understand why this java program doesn't find array from json file. I didn't find similar type json file via google so pls educate me.

Error:

C:\temp\example.json
org.json.JSONException: JSONObject["result"] not found.
    at org.json.JSONObject.get(JSONObject.java:572)
    at org.json.JSONObject.getJSONArray(JSONObject.java:765)
    at JsonParsingMachine.main(JsonParsingMachine.java:17)

.java content:

import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.json.*;

public class JsonParsingMachine {

    public static void main(String[] args) {
        String tiedosto = "C:/temp/example.json";

        System.out.println(Paths.get(tiedosto));
        try {
            String contents = new String((Files.readAllBytes(Paths.get(tiedosto))));
            JSONObject o = new JSONObject(contents);
            JSONArray res = o.getJSONArray("result");

            for (int i = 0; i < res.length(); i++) {
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

json file (example.json)

{
  "quoteResponse" : {
    "result" : [ {
      "language" : "en-US",
      "region" : "US",
      "quoteType" : "EQUITY",
      "quoteSourceName" : "Nasdaq Real Time Price",
      "triggerable" : true
    } ]
  }
}
1
  • o will be a JSONObject with a field "quoteResponse". Use your debugger to examine o. Commented Feb 15, 2021 at 6:50

1 Answer 1

2

result array is inside quoteResponse JSONObject. You need to do this instead:

JSONObject o = new JSONObject(contents);
JSONObject quoteResponse = o.getJSONObject("quoteResponse");
JSONArray res = quoteResponse.getJSONArray("result");
Sign up to request clarification or add additional context in comments.

1 Comment

Thx, got this now.

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.