-1

Can someone help me with passing below JSON data in body for my HTTP POST call..

{
    "name": "ABC",
    "user": "fl9f03fe24a2c4a4b51a4d75",
    "data": 
    {
        "details": "component",
        "Key": "123",
        "region": "server-23"
    }

}
Vipin CP
  • 3,642
  • 3
  • 33
  • 55
Altamash Shaikh
  • 99
  • 1
  • 1
  • 7
  • What you have tried so far? share your code. – Musakkhir Sayyed Jul 28 '16 at 05:39
  • where are you making this POST call ? are you getting any error ? – Srivatsa N Jul 28 '16 at 05:49
  • A probable answer is already in the [link](http://stackoverflow.com/questions/7181534/http-post-using-json-in-java) – Roushan Kumar Jul 28 '16 at 06:28
  • @MusakkhirSayyed : I am creating below JSON data to pass in my HTTP call but unable to add values to object "data" below is the code. `JSONObject obj = new JSONObject(); obj.put("name", "ABC"); obj.put("user","fl9f03fe24a2c4a4b51a4d75"); datanew = (JSONObject) obj.get("data"); datanew.put("details", "component"); datanew.put("Key", "123"); datanew.put("region","server-23")` – Altamash Shaikh Jul 28 '16 at 07:03
  • Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – Raedwald Dec 29 '20 at 15:49

1 Answers1

0

So this is your code (I edited your post, please accept it):

JSONObject obj = new JSONObject();
obj.put("name", "ABC");
obj.put("user","fl9f03fe24a2c4a4b51a4d75");
datanew = (JSONObject) obj.get("data");
datanew.put("details", "component");
datanew.put("Key", "123");
datanew.put("region","server-23");

Your JSON object obj is new and empty, it doesn't contain anything. So you can't get("data") before you put("data") first. Try this:

JSONObject obj = new JSONObject();
obj.put("name", "ABC");
obj.put("user","fl9f03fe24a2c4a4b51a4d75");

JSONObject datanew = new JSONObject();
datanew.put("details", "component");
datanew.put("Key", "123");
datanew.put("region","server-23");

obj.put("data", datanew);
walen
  • 7,103
  • 2
  • 37
  • 58