JSONobject inside JSONObject in java












0















I have following request mapping:



@RequestMapping(value = "/reCalculated", method = RequestMethod.POST)
public @ResponseBody void reCalculated(JSONObject obj) {

obj.
}


and then i have incoming json



{"params":{"date_a":"2017-05-01","date_b":"2017-05-02"}}


but in java, obj. only gives me options toString() and toJSONString()
meanwhile all the tutorials, and few threads clearly tell me i should be able to do obj.getJSONObject("params") Why is this? how can i access my parameters?



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


is the dependency.










share|improve this question























  • Why are you not considering defining a POJO data model and map the @RequestBody to it?

    – Brad
    Nov 23 '18 at 8:44
















0















I have following request mapping:



@RequestMapping(value = "/reCalculated", method = RequestMethod.POST)
public @ResponseBody void reCalculated(JSONObject obj) {

obj.
}


and then i have incoming json



{"params":{"date_a":"2017-05-01","date_b":"2017-05-02"}}


but in java, obj. only gives me options toString() and toJSONString()
meanwhile all the tutorials, and few threads clearly tell me i should be able to do obj.getJSONObject("params") Why is this? how can i access my parameters?



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


is the dependency.










share|improve this question























  • Why are you not considering defining a POJO data model and map the @RequestBody to it?

    – Brad
    Nov 23 '18 at 8:44














0












0








0








I have following request mapping:



@RequestMapping(value = "/reCalculated", method = RequestMethod.POST)
public @ResponseBody void reCalculated(JSONObject obj) {

obj.
}


and then i have incoming json



{"params":{"date_a":"2017-05-01","date_b":"2017-05-02"}}


but in java, obj. only gives me options toString() and toJSONString()
meanwhile all the tutorials, and few threads clearly tell me i should be able to do obj.getJSONObject("params") Why is this? how can i access my parameters?



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


is the dependency.










share|improve this question














I have following request mapping:



@RequestMapping(value = "/reCalculated", method = RequestMethod.POST)
public @ResponseBody void reCalculated(JSONObject obj) {

obj.
}


and then i have incoming json



{"params":{"date_a":"2017-05-01","date_b":"2017-05-02"}}


but in java, obj. only gives me options toString() and toJSONString()
meanwhile all the tutorials, and few threads clearly tell me i should be able to do obj.getJSONObject("params") Why is this? how can i access my parameters?



<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>


is the dependency.







java json spring-boot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:34









ClomezClomez

339214




339214













  • Why are you not considering defining a POJO data model and map the @RequestBody to it?

    – Brad
    Nov 23 '18 at 8:44



















  • Why are you not considering defining a POJO data model and map the @RequestBody to it?

    – Brad
    Nov 23 '18 at 8:44

















Why are you not considering defining a POJO data model and map the @RequestBody to it?

– Brad
Nov 23 '18 at 8:44





Why are you not considering defining a POJO data model and map the @RequestBody to it?

– Brad
Nov 23 '18 at 8:44












2 Answers
2






active

oldest

votes


















1














json-simple library had method get(String name) and need external type casting like below



String name = (String) jsonObject.get("name");
JSONArray msg = (JSONArray) jsonObject.get("messages");
long age = (Long) jsonObject.get("age");


But gson library has predefined methods here



public JsonObject getAsJsonObject(String memberName)
public JsonArray getAsJsonArray(String memberName)


Maven dependency



<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.3</version>
</dependency>





share|improve this answer

































    1














    You can get JSONString from your param



    Gson gson = (new GsonBuilder()).create();
    jsonString = obj.get("params").getAsString();
    JsonObject param= gson.fromJson(jsonString, JsonObject.class);


    As Deadpool said, you want to use gson but you added json-simple dependency.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442403%2fjsonobject-inside-jsonobject-in-java%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      json-simple library had method get(String name) and need external type casting like below



      String name = (String) jsonObject.get("name");
      JSONArray msg = (JSONArray) jsonObject.get("messages");
      long age = (Long) jsonObject.get("age");


      But gson library has predefined methods here



      public JsonObject getAsJsonObject(String memberName)
      public JsonArray getAsJsonArray(String memberName)


      Maven dependency



      <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
      <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.3</version>
      </dependency>





      share|improve this answer






























        1














        json-simple library had method get(String name) and need external type casting like below



        String name = (String) jsonObject.get("name");
        JSONArray msg = (JSONArray) jsonObject.get("messages");
        long age = (Long) jsonObject.get("age");


        But gson library has predefined methods here



        public JsonObject getAsJsonObject(String memberName)
        public JsonArray getAsJsonArray(String memberName)


        Maven dependency



        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.3</version>
        </dependency>





        share|improve this answer




























          1












          1








          1







          json-simple library had method get(String name) and need external type casting like below



          String name = (String) jsonObject.get("name");
          JSONArray msg = (JSONArray) jsonObject.get("messages");
          long age = (Long) jsonObject.get("age");


          But gson library has predefined methods here



          public JsonObject getAsJsonObject(String memberName)
          public JsonArray getAsJsonArray(String memberName)


          Maven dependency



          <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
          <dependency>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
          <version>2.8.3</version>
          </dependency>





          share|improve this answer















          json-simple library had method get(String name) and need external type casting like below



          String name = (String) jsonObject.get("name");
          JSONArray msg = (JSONArray) jsonObject.get("messages");
          long age = (Long) jsonObject.get("age");


          But gson library has predefined methods here



          public JsonObject getAsJsonObject(String memberName)
          public JsonArray getAsJsonArray(String memberName)


          Maven dependency



          <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
          <dependency>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
          <version>2.8.3</version>
          </dependency>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 7:49

























          answered Nov 23 '18 at 7:38









          DeadpoolDeadpool

          7,4172829




          7,4172829

























              1














              You can get JSONString from your param



              Gson gson = (new GsonBuilder()).create();
              jsonString = obj.get("params").getAsString();
              JsonObject param= gson.fromJson(jsonString, JsonObject.class);


              As Deadpool said, you want to use gson but you added json-simple dependency.






              share|improve this answer




























                1














                You can get JSONString from your param



                Gson gson = (new GsonBuilder()).create();
                jsonString = obj.get("params").getAsString();
                JsonObject param= gson.fromJson(jsonString, JsonObject.class);


                As Deadpool said, you want to use gson but you added json-simple dependency.






                share|improve this answer


























                  1












                  1








                  1







                  You can get JSONString from your param



                  Gson gson = (new GsonBuilder()).create();
                  jsonString = obj.get("params").getAsString();
                  JsonObject param= gson.fromJson(jsonString, JsonObject.class);


                  As Deadpool said, you want to use gson but you added json-simple dependency.






                  share|improve this answer













                  You can get JSONString from your param



                  Gson gson = (new GsonBuilder()).create();
                  jsonString = obj.get("params").getAsString();
                  JsonObject param= gson.fromJson(jsonString, JsonObject.class);


                  As Deadpool said, you want to use gson but you added json-simple dependency.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 7:48









                  htpvlhtpvl

                  647310




                  647310






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442403%2fjsonobject-inside-jsonobject-in-java%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown