Here's a simpler (but not so short) version that does not require try-catch:
Map<String, String> data = new HashMap<>(); data.put("user", "mark@facebook.com"); data.put("pass", "123"); JSONObject jsonData = new JSONObject(data);
If you want to add jsonObject to the field, you can do this:
data.put("socialMedia", (new JSONObject()).put("facebookId", "1174989895893400")); data.put("socialMedia", (new JSONObject()).put("googleId", "106585039098745627377"));
Unfortunately, the put () method needs a try-catch.
IF , you want to avoid try-catch again (not very recommended, but it is ok if you can guarantee well-formed json strings), you can do this:
data.put("socialMedia", "{ 'facebookId': '1174989895893400' }");
You can do the same about JsonArrays and so on.
Greetings.
felippe Jan 26 '17 at 1:54 on 2017-01-26 01:54
source share