HukeLau_DABA
HukeLau_DABA

Reputation: 2528

Creating a jsonObject that contains another jsonObject

I am using asynchttpclient to do a POST and am constructing a body like this:

{
  "params": {
                  "firstname": "%Paul%"
            }
}

The following sometimes produces the right body, sometimes produces an empty body:

 String encodedFirstname = "%" + first + "%";
 JSONObject paramsVal = new JSONObject();
 paramsVal.put("firstname", encodedFirstname);
 String[] keys = { "params" };
 JSONObject postBody = new JSONObject(paramsVal, keys);

What do I need to correct? Thank you.

Upvotes: 0

Views: 915

Answers (2)

Fady Ibrahim
Fady Ibrahim

Reputation: 382

This works for me:

JSONObject object1 = new JSONObject();
try {
        object1.put("firstname", "%Paul%");
} catch (JSONException e) {
        e.printStackTrace();
}
JSONObject object2 = new JSONObject();
try {
        object2.putOpt("params", object1);
} catch (JSONException e) {
        e.printStackTrace();
}

Upvotes: 0

HukeLau_DABA
HukeLau_DABA

Reputation: 2528

I tried using put again and it worked:

JSONObject postBody = new JSONObject("params", paramsVal);

Worst. Javadoc. Ever.

Upvotes: -1

Related Questions