JSONobject inside JSONObject in java
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
add a comment |
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
Why are you not considering defining a POJO data model and map the @RequestBody to it?
– Brad
Nov 23 '18 at 8:44
add a comment |
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
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
java json spring-boot
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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>
add a comment |
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.
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%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
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>
add a comment |
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>
add a comment |
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>
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>
edited Nov 23 '18 at 7:49
answered Nov 23 '18 at 7:38
DeadpoolDeadpool
7,4172829
7,4172829
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 7:48
htpvlhtpvl
647310
647310
add a comment |
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%2f53442403%2fjsonobject-inside-jsonobject-in-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
Why are you not considering defining a POJO data model and map the @RequestBody to it?
– Brad
Nov 23 '18 at 8:44