-6

I need to build a JSONObject that looks like this:

{ 
    "login": {
        "email": "blah@blah.com",
        "password": "password"
    }
}

I understand that I'm supposed to use jsonObject.put("key", "value), but how to I get the parent "login" node with nested keys / values?

Will I have an easier time building objects like this with Gson? Some of them are pretty complex. Thanks!

botbot
  • 7,299
  • 14
  • 58
  • 96

2 Answers2

4

In pure "android-java", this would look like that:

JSONObject data = new JSONObject();
data.put("email", "blah@blah.com");
data.put("password", "password");

JSONObject login = new JSONObject();
login.put("login", data);

Then you'll have your JSON object.

A good idea is to read some articles about JSON and Android:

  1. JSONObject javadoc
  2. Vogella's article
  3. stackoverflow.com questions
Community
  • 1
  • 1
Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
0

Try this

JSONObject mainObj = new JSONObject();

        JSONObject innerObj = new JSONObject();

        innerObj.put("email","blah@blah.com");
        innerObj.put("password","password");

        mainObj.put("login", innerObj);

        System.out.println("JSON : "  +mainObj);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77