-1

this is my depedency

 <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
  <version>20160810</version>
</dependency>

this is my json object

{
   "authorize": {
       "balance": "9984.06",
       "country": "id",
       "currency": "USD",
       "email": "xxxx.xxxx@gmail.com",
       "fullname": "  ",
       "is_virtual": 1,
       "landing_company_fullname": "Binary Ltd",
       "landing_company_name": "virtual",
       "loginid": "VRg4423",
       "scopes": [
         "read",
         "trade"
       ]
   }
}

this is my code

JSONObject jsonObject = new JSONObject(fil);
JSONObject jsonChildObject = (JSONObject) jsonObject.get("authorize");
Iterator iterator  = jsonChildObject.keys();
String key = null;
while (iterator.hasNext()) {
    key = (String) iterator.next();
    System.out.println("balance value:" + ((JSONObject) jsonChildObject.get(key)).get("balance"));
}

I have no idea why I can't acces all the child node value. It returns null pointer exception there is no "authorize" i can't get the parent value.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
septian
  • 1
  • 1
  • Can you share the stack trace – Lalit Rao Oct 29 '16 at 07:15
  • org.json.JSONException: JSONObject["authorize"] not found. at org.json.JSONObject.get(JSONObject.java:471) at com.belgedezbot.coba.main(coba.java:32) – septian Oct 29 '16 at 07:17
  • sure this is the stack trace – septian Oct 29 '16 at 07:17
  • So it's not a NullPointerException, actually? Post that in the question. And we have no idea what `fil`is. Post a complete minimal example reproducing the problem. My advice: use your debugger. Or at least print the contents of your variables to know what's going on. – JB Nizet Oct 29 '16 at 07:20
  • fil is variable file reader for the json file FileReader fil = new FileReader – septian Oct 29 '16 at 07:22

1 Answers1

0

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.
GameDroids
  • 5,584
  • 6
  • 40
  • 59