How would I handle “sub strings?” JSON files using java?
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
add a comment |
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
add a comment |
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
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
java json
edited Nov 25 '18 at 22:54
Robert Harvey♦
149k33275420
149k33275420
asked Nov 25 '18 at 22:23
loserloser
368
368
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you! This looks like it will work.
– loser
Nov 26 '18 at 15:20
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thank you! This looks like it will work.
– loser
Nov 26 '18 at 15:20
add a comment |
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
Thank you! This looks like it will work.
– loser
Nov 26 '18 at 15:20
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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