Let's have a look at your code and we'll find the problem:
JSONObject jsonObject = new JSONObject(fil);
// jsonObject is now your whole json
// {
// "authorize":
// ...
// }
JSONObject jsonChildObject = (JSONObject) jsonObject.get("authorize");
// the "authorize" child node is now contained in jsonChildObject
// {
// "balance": "9984.06",
// "country": "id",
// "currency": "USD",
// ...
// }
Iterator iterator = jsonChildObject.keys();
// jsonChildObject.keys() iterates over this set:
// ["balance", "country", "currency" ... ]
String key = null;
while (iterator.hasNext()) {
key = (String) iterator.next();
// the very first iteration, key will have the value "balance"
System.out.println("balance value:"
+ ((JSONObject) jsonChildObject.get(key)).get("balance"));
// and here you try to get the element "balance" from the element with the key "balance"
}
Basically the line
((JSONObject) jsonChildObject.get(key)).get("balance"))
looks like this during the first iteration:
((JSONObject) jsonChildObject.get("balance")).get("balance"))
which of course does not exist.
1) if you only want the "balance", don't iterate over the keys and remove the while
loop. Just write jsonChildObject.get("balance")
2) if you need to get ALL children and you want to keep the while
loop, the you could do a switch over they keys:
while (iterator.hasNext()) {
key = (String) iterator.next();
switch(key){
case "balance":
float balance = jsonChildObject.get(key);
break;
case "country":
...
}
It's either that, or some other library (like Gson) that automatically maps your json in a simple POJO that you wrote before
Maven dependency for Gson
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
But I would go for 1) and simply get all your values by manually calling jsonChildObject.get("balance")
or ...get("country");
and so on.
EDIT
I just realized you have problems getting "authorize", maybe a simple print
would do to show you how your json looks like:
System.out.println(jsonObject.toString(3)); // should give you a nicely printed JSON. If this is already `null` then the problem is already in `fil` which might be the wrong json.