JSON root elements and encoding
I'm having a JSON deserialized from Java as follow:
Java
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);
JSON
"accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...
The deserialized Java POJO being:
public class GraphDTO {
private Set<Account> accounts = new HashSet<>();
public Set<Account> getAccounts() {
return accounts;
}
}
Questions
- How can I remove "accounts" from the generated JSON (first line) ?
- Injecting the JSON form into JavaScript, I'm getting an encoded form like:
var data = { "accounts" : [ {
...
How can I avoid this ?
javascript java json
add a comment |
I'm having a JSON deserialized from Java as follow:
Java
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);
JSON
"accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...
The deserialized Java POJO being:
public class GraphDTO {
private Set<Account> accounts = new HashSet<>();
public Set<Account> getAccounts() {
return accounts;
}
}
Questions
- How can I remove "accounts" from the generated JSON (first line) ?
- Injecting the JSON form into JavaScript, I'm getting an encoded form like:
var data = { "accounts" : [ {
...
How can I avoid this ?
javascript java json
How are you parsing JSON in javascript? Can you provide any code?
– theapologist
Nov 26 '18 at 13:22
Yes:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
– Hey StackExchange
Nov 26 '18 at 13:26
add a comment |
I'm having a JSON deserialized from Java as follow:
Java
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);
JSON
"accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...
The deserialized Java POJO being:
public class GraphDTO {
private Set<Account> accounts = new HashSet<>();
public Set<Account> getAccounts() {
return accounts;
}
}
Questions
- How can I remove "accounts" from the generated JSON (first line) ?
- Injecting the JSON form into JavaScript, I'm getting an encoded form like:
var data = { "accounts" : [ {
...
How can I avoid this ?
javascript java json
I'm having a JSON deserialized from Java as follow:
Java
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO);
JSON
"accounts" : [ {
"name" : "1009427721",
"value" : 16850.79,
"children" : [ {
"name" : "BITCOIN EARNINGS",
"value" : 10734.24,
"children" : [ {
"name" : "2017",
"value" : 1037.82,
"children" : [ {
"name" : "07",
"value" : 518.91
} ]
} ]
}, ...
The deserialized Java POJO being:
public class GraphDTO {
private Set<Account> accounts = new HashSet<>();
public Set<Account> getAccounts() {
return accounts;
}
}
Questions
- How can I remove "accounts" from the generated JSON (first line) ?
- Injecting the JSON form into JavaScript, I'm getting an encoded form like:
var data = { "accounts" : [ {
...
How can I avoid this ?
javascript java json
javascript java json
edited Nov 26 '18 at 13:36
Hey StackExchange
asked Nov 26 '18 at 13:20
Hey StackExchangeHey StackExchange
1,325717
1,325717
How are you parsing JSON in javascript? Can you provide any code?
– theapologist
Nov 26 '18 at 13:22
Yes:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
– Hey StackExchange
Nov 26 '18 at 13:26
add a comment |
How are you parsing JSON in javascript? Can you provide any code?
– theapologist
Nov 26 '18 at 13:22
Yes:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
– Hey StackExchange
Nov 26 '18 at 13:26
How are you parsing JSON in javascript? Can you provide any code?
– theapologist
Nov 26 '18 at 13:22
How are you parsing JSON in javascript? Can you provide any code?
– theapologist
Nov 26 '18 at 13:22
Yes:
<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
– Hey StackExchange
Nov 26 '18 at 13:26
Yes:
<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
– Hey StackExchange
Nov 26 '18 at 13:26
add a comment |
2 Answers
2
active
oldest
votes
I don't think it's possible to avoid the accounts
, but you can do this:
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());
We're waiting your way to parse JSON in javascript...
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)
– Hey StackExchange
Nov 26 '18 at 13:31
Why don't you useJSON.parse(graphDTOJSON)
instead?
– theapologist
Nov 26 '18 at 13:32
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing nowvar data = ${graphDTOJSON};
work.
– Hey StackExchange
Nov 26 '18 at 13:53
add a comment |
Got it work as follow:
Applying the answer of Mohicane:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())
Injecting the JSON from Java to JavaScript (in a JSP) with:
var data = ${graphDTOJSON};
Many thanks!
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
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%2f53482032%2fjson-root-elements-and-encoding%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
I don't think it's possible to avoid the accounts
, but you can do this:
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());
We're waiting your way to parse JSON in javascript...
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)
– Hey StackExchange
Nov 26 '18 at 13:31
Why don't you useJSON.parse(graphDTOJSON)
instead?
– theapologist
Nov 26 '18 at 13:32
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing nowvar data = ${graphDTOJSON};
work.
– Hey StackExchange
Nov 26 '18 at 13:53
add a comment |
I don't think it's possible to avoid the accounts
, but you can do this:
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());
We're waiting your way to parse JSON in javascript...
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)
– Hey StackExchange
Nov 26 '18 at 13:31
Why don't you useJSON.parse(graphDTOJSON)
instead?
– theapologist
Nov 26 '18 at 13:32
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing nowvar data = ${graphDTOJSON};
work.
– Hey StackExchange
Nov 26 '18 at 13:53
add a comment |
I don't think it's possible to avoid the accounts
, but you can do this:
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());
We're waiting your way to parse JSON in javascript...
I don't think it's possible to avoid the accounts
, but you can do this:
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts());
We're waiting your way to parse JSON in javascript...
answered Nov 26 '18 at 13:25
MohicaneMohicane
9210
9210
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)
– Hey StackExchange
Nov 26 '18 at 13:31
Why don't you useJSON.parse(graphDTOJSON)
instead?
– theapologist
Nov 26 '18 at 13:32
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing nowvar data = ${graphDTOJSON};
work.
– Hey StackExchange
Nov 26 '18 at 13:53
add a comment |
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)
– Hey StackExchange
Nov 26 '18 at 13:31
Why don't you useJSON.parse(graphDTOJSON)
instead?
– theapologist
Nov 26 '18 at 13:32
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing nowvar data = ${graphDTOJSON};
work.
– Hey StackExchange
Nov 26 '18 at 13:53
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:
<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)– Hey StackExchange
Nov 26 '18 at 13:31
Thanks, your approach is giving me the structure I was looking for :) For the use in JavaScript, I do:
<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
to be consume by a sunburst char(docs.anychart.com/Basic_Charts/Sunburst_Chart)– Hey StackExchange
Nov 26 '18 at 13:31
Why don't you use
JSON.parse(graphDTOJSON)
instead?– theapologist
Nov 26 '18 at 13:32
Why don't you use
JSON.parse(graphDTOJSON)
instead?– theapologist
Nov 26 '18 at 13:32
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now
var data = ${graphDTOJSON};
work.– Hey StackExchange
Nov 26 '18 at 13:53
Parse will return an Object. Tried your approach with JSON.stringify, don't work. but passing now
var data = ${graphDTOJSON};
work.– Hey StackExchange
Nov 26 '18 at 13:53
add a comment |
Got it work as follow:
Applying the answer of Mohicane:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())
Injecting the JSON from Java to JavaScript (in a JSP) with:
var data = ${graphDTOJSON};
Many thanks!
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
add a comment |
Got it work as follow:
Applying the answer of Mohicane:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())
Injecting the JSON from Java to JavaScript (in a JSP) with:
var data = ${graphDTOJSON};
Many thanks!
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
add a comment |
Got it work as follow:
Applying the answer of Mohicane:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())
Injecting the JSON from Java to JavaScript (in a JSP) with:
var data = ${graphDTOJSON};
Many thanks!
Got it work as follow:
Applying the answer of Mohicane:
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(graphDTO.getAccounts())
Injecting the JSON from Java to JavaScript (in a JSP) with:
var data = ${graphDTOJSON};
Many thanks!
answered Nov 26 '18 at 13:57
Hey StackExchangeHey StackExchange
1,325717
1,325717
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
add a comment |
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
Is it possible you flag my answer as the right one if you think it's good?
– Mohicane
Nov 28 '18 at 16:48
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%2f53482032%2fjson-root-elements-and-encoding%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
How are you parsing JSON in javascript? Can you provide any code?
– theapologist
Nov 26 '18 at 13:22
Yes:
<script> var data = <c:out value="${graphDTOJSON}"/>; ... </script>
– Hey StackExchange
Nov 26 '18 at 13:26