简体   繁体   中英

creating nested JSON object in Java and passing it as request using RestPost method?

I want to build a JSON Object similar to the following structure in java, and then pass it as a request using the restPost method.

    {
      "fields": [
       {
          "SESSION_SESSIONNUMID": "500"
       },
       {
          "SESSION_STATUS": "BP"
       },
       {
          "SESSION_DESCRIPTION": "Updated"
       },
       {
          "SESSION_SESSIONDATE": "2016-07-20"
       },
       {
          "SESSION_CURRENCY_TYPE": "USD"
       }
     ]
   }

So this is what I did ,

    public void Insert() {
   try {
    String lstrPath = Constants.MIP_BASE_URI + "/api/te/JV/sessions";

   System.out.println("Path for creation of session :\n " + lstrPath);

  //create the JSON object containing the new contacts details.
    JSONObject array = new JSONObject();
    JSONObject object = new JSONObject();

    JSONObject A1 = new JSONObject();
    A1.put("SESSION_SESSIONNUMID " , "100 ");

   JSONObject A2 = new JSONObject();
   A2.put("SESSION_STATUS " , "BP");

   JSONObject A3 = new JSONObject();
   A3.put("SESSION_DESCRIPTION " , "CODING");

   JSONObject A4 = new JSONObject();
   A4.put("SESSION_SESSIONDATE" , "2016-10-20");

   JSONObject A5 = new JSONObject();
   A5.put("SESSION_CURRENCY_TYPE" , "USD");
   object.put("def", array);

   System.out.println("Passing request :\n" + A1.toString(1));

   JSONObject ljsonResponse = RestCalls.RestPost(lstrPath,
   A1, Constants.REQUESTING_CLASS.MIP);

   if (ljsonResponse != null) {
   Constants.MIP_TOKEN = ljsonResponse.getString("token");
   }
   System.out.println("Token from response: " + Constants.MIP_TOKEN);
      Constants.MIP_AUTH_HEADER = new BasicHeader("Authorization-Token",
        Constants.MIP_TOKEN);

    } catch (JSONException ex) {
      Logger.getLogger(ConnectToMip.class.getName()).log(Level.SEVERE, null,ex);
    }
  }

And now the issue is that In the above code I am able to create multiple JSON Objects required to create a session , but now I am unable to figure out how do I send(pass) multiple JSON objects in the response.

Note : In the last line of code below, I send 1 JSON Object(A1) as a response, similarly I need to send multiple JSON Objects that I created (A2, A3 , A4 , A5) in the response for a successful POST .

First I convert my complex json into java like this,

JSONArray json = new JSONArray();
JSONObject Final = new JSONObject();

JSONObject A1 = new JSONObject();
      A1.put("SESSION_SESSIONNUMID" , "212");

      JSONObject A2 = new JSONObject();
      A2.put("SESSION_STATUS" , "BP");

      JSONObject A3 = new JSONObject();
      A3.put("SESSION_DESCRIPTION" , "CODING");

      JSONObject A4 = new JSONObject();
      A4.put("SESSION_SESSIONDATE" , "2016-10-20");

      JSONObject A5 = new JSONObject();
      A5.put("SESSION_CURRENCY_TYPE" , "USD");

    json.put(A1);
    json.put(A2);
    json.put(A3);
    json.put(A4);
    json.put(A5);

      Final.put("fields",json);

and then I pass Final into my tostring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM