How would I handle “sub strings?” JSON files using java?












1















I'm not sure if "complex JSON" is the right terminology.



I'm trying to go through the text in this JSON



https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson



When handling the simple stuff like metadata generated I do something like this:



  JSONObject x = JSONObject.fromObject(JSonString);
JSONObject petData = (JSONObject) (x.get("metadata"));

System.out.println(petData.get("generated"));


But when I tried doing more complicated stuff like finding the features: 0: type: I couldn't get it to work. None of the examples I found have included something like this. Where it has more than one step to get to it. Also, if you know the actual name for JSON files that do this instead of me just calling that complex that'd be great too!










share|improve this question





























    1















    I'm not sure if "complex JSON" is the right terminology.



    I'm trying to go through the text in this JSON



    https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson



    When handling the simple stuff like metadata generated I do something like this:



      JSONObject x = JSONObject.fromObject(JSonString);
    JSONObject petData = (JSONObject) (x.get("metadata"));

    System.out.println(petData.get("generated"));


    But when I tried doing more complicated stuff like finding the features: 0: type: I couldn't get it to work. None of the examples I found have included something like this. Where it has more than one step to get to it. Also, if you know the actual name for JSON files that do this instead of me just calling that complex that'd be great too!










    share|improve this question



























      1












      1








      1








      I'm not sure if "complex JSON" is the right terminology.



      I'm trying to go through the text in this JSON



      https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson



      When handling the simple stuff like metadata generated I do something like this:



        JSONObject x = JSONObject.fromObject(JSonString);
      JSONObject petData = (JSONObject) (x.get("metadata"));

      System.out.println(petData.get("generated"));


      But when I tried doing more complicated stuff like finding the features: 0: type: I couldn't get it to work. None of the examples I found have included something like this. Where it has more than one step to get to it. Also, if you know the actual name for JSON files that do this instead of me just calling that complex that'd be great too!










      share|improve this question
















      I'm not sure if "complex JSON" is the right terminology.



      I'm trying to go through the text in this JSON



      https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson



      When handling the simple stuff like metadata generated I do something like this:



        JSONObject x = JSONObject.fromObject(JSonString);
      JSONObject petData = (JSONObject) (x.get("metadata"));

      System.out.println(petData.get("generated"));


      But when I tried doing more complicated stuff like finding the features: 0: type: I couldn't get it to work. None of the examples I found have included something like this. Where it has more than one step to get to it. Also, if you know the actual name for JSON files that do this instead of me just calling that complex that'd be great too!







      java json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 22:54









      Robert Harvey

      149k33275420




      149k33275420










      asked Nov 25 '18 at 22:23









      loserloser

      368




      368
























          1 Answer
          1






          active

          oldest

          votes


















          2














          While working with marshalling and unmarshalling, it is always good to have a POJO model defined. In your case, it would be something like this:



          class GeoResponse {
          private String type;
          private Metadata metadata;
          private List<Feature> features;

          // so on
          // getters and setters
          }

          class Metadata {
          private String generated;
          private String url;
          // so on
          // getters and setters
          }
          class Feature {
          private String id;
          private String type;
          private Properties properties;
          // so on
          }
          // so on


          Parse JSON to your object with Jackson Mapper as:



          ObjectMapper mapper = new ObjectMapper();
          GeoResponse data = mapper.readValue(jsonString, GeoResponse.class);


          Now you can easily access the fields you want. For example:



          Metadata metadata = data.getMetadata();
          // ...and so on





          share|improve this answer


























          • Thank you! This looks like it will work.

            – loser
            Nov 26 '18 at 15:20











          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%2f53472615%2fhow-would-i-handle-sub-strings-json-files-using-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          While working with marshalling and unmarshalling, it is always good to have a POJO model defined. In your case, it would be something like this:



          class GeoResponse {
          private String type;
          private Metadata metadata;
          private List<Feature> features;

          // so on
          // getters and setters
          }

          class Metadata {
          private String generated;
          private String url;
          // so on
          // getters and setters
          }
          class Feature {
          private String id;
          private String type;
          private Properties properties;
          // so on
          }
          // so on


          Parse JSON to your object with Jackson Mapper as:



          ObjectMapper mapper = new ObjectMapper();
          GeoResponse data = mapper.readValue(jsonString, GeoResponse.class);


          Now you can easily access the fields you want. For example:



          Metadata metadata = data.getMetadata();
          // ...and so on





          share|improve this answer


























          • Thank you! This looks like it will work.

            – loser
            Nov 26 '18 at 15:20
















          2














          While working with marshalling and unmarshalling, it is always good to have a POJO model defined. In your case, it would be something like this:



          class GeoResponse {
          private String type;
          private Metadata metadata;
          private List<Feature> features;

          // so on
          // getters and setters
          }

          class Metadata {
          private String generated;
          private String url;
          // so on
          // getters and setters
          }
          class Feature {
          private String id;
          private String type;
          private Properties properties;
          // so on
          }
          // so on


          Parse JSON to your object with Jackson Mapper as:



          ObjectMapper mapper = new ObjectMapper();
          GeoResponse data = mapper.readValue(jsonString, GeoResponse.class);


          Now you can easily access the fields you want. For example:



          Metadata metadata = data.getMetadata();
          // ...and so on





          share|improve this answer


























          • Thank you! This looks like it will work.

            – loser
            Nov 26 '18 at 15:20














          2












          2








          2







          While working with marshalling and unmarshalling, it is always good to have a POJO model defined. In your case, it would be something like this:



          class GeoResponse {
          private String type;
          private Metadata metadata;
          private List<Feature> features;

          // so on
          // getters and setters
          }

          class Metadata {
          private String generated;
          private String url;
          // so on
          // getters and setters
          }
          class Feature {
          private String id;
          private String type;
          private Properties properties;
          // so on
          }
          // so on


          Parse JSON to your object with Jackson Mapper as:



          ObjectMapper mapper = new ObjectMapper();
          GeoResponse data = mapper.readValue(jsonString, GeoResponse.class);


          Now you can easily access the fields you want. For example:



          Metadata metadata = data.getMetadata();
          // ...and so on





          share|improve this answer















          While working with marshalling and unmarshalling, it is always good to have a POJO model defined. In your case, it would be something like this:



          class GeoResponse {
          private String type;
          private Metadata metadata;
          private List<Feature> features;

          // so on
          // getters and setters
          }

          class Metadata {
          private String generated;
          private String url;
          // so on
          // getters and setters
          }
          class Feature {
          private String id;
          private String type;
          private Properties properties;
          // so on
          }
          // so on


          Parse JSON to your object with Jackson Mapper as:



          ObjectMapper mapper = new ObjectMapper();
          GeoResponse data = mapper.readValue(jsonString, GeoResponse.class);


          Now you can easily access the fields you want. For example:



          Metadata metadata = data.getMetadata();
          // ...and so on






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 23:06









          Robert Harvey

          149k33275420




          149k33275420










          answered Nov 25 '18 at 22:56









          Yogen RaiYogen Rai

          1,32521224




          1,32521224













          • Thank you! This looks like it will work.

            – loser
            Nov 26 '18 at 15:20



















          • Thank you! This looks like it will work.

            – loser
            Nov 26 '18 at 15:20

















          Thank you! This looks like it will work.

          – loser
          Nov 26 '18 at 15:20





          Thank you! This looks like it will work.

          – loser
          Nov 26 '18 at 15:20




















          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%2f53472615%2fhow-would-i-handle-sub-strings-json-files-using-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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'