generate json to a unique custom classSerializing to JSON in jQueryHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?How do I generate random integers within a specific range in Java?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
Single word for "refusing to move to next activity unless present one is completed."
Are randomly-generated passwords starting with "a" less secure?
For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?
PIC12F675 GP4 doesn't work
Is anyone advocating the promotion of homosexuality in UK schools?
Domain of definition of Laplace Operator on L^2
How is angular momentum conserved for the orbiting body if the centripetal force disappears?
Can I call 112 to check a police officer's identity in the Czech Republic?
Is purchasing foreign currency before going abroad a losing proposition?
Can I intentionally omit previous work experience or pretend it doesn't exist when applying for jobs?
Who Can Help Retag This?
Referring to different instances of the same character in time travel
Did any of the founding fathers anticipate Lysander Spooner's criticism of the constitution?
How do you create draggable points inside a graphic image?
How do Windows version numbers work?
Should disabled buttons give feedback when clicked?
Storming Area 51
Simple LED driver, transistor and GPIO
How to achieve this rough borders and stippled illustration look?
Extract an attribute value from XML
How might the United Kingdom become a republic?
Shortest distance around a pyramid?
Why didn't Thanos kill all the Dwarves on Nidavellir?
How can an advanced civilization forget how to manufacture its technology?
generate json to a unique custom class
Serializing to JSON in jQueryHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?How do I generate random integers within a specific range in Java?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am very new to java and just started with json...
I have the following json file:
"step1":
"version": 1,
"items":
"run": false,
"jump": true
,
"step2":
"version": "None",
"items":
"happy": true,
"sad": false
I am using Gson in my main like this:
Gson gs = new Gson();
Content tmp = gs.fromJson(<json string>, Content.class);
my class:
public class Content
@SerializedName("step1")
private Step step1;
@SerializedName("step2")
private Step step2;
each step class:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
???????
as you can see the "?????" part is what I am trying to understand - How can I convert the items without needing to know the field name..? meaning to a HashMap/another iterable object..? can I initialize using a method..?
I have tried creating an Item class with a constructor but I do not understand how to use it in this case..
java json serialization initialization gson
add a comment |
I am very new to java and just started with json...
I have the following json file:
"step1":
"version": 1,
"items":
"run": false,
"jump": true
,
"step2":
"version": "None",
"items":
"happy": true,
"sad": false
I am using Gson in my main like this:
Gson gs = new Gson();
Content tmp = gs.fromJson(<json string>, Content.class);
my class:
public class Content
@SerializedName("step1")
private Step step1;
@SerializedName("step2")
private Step step2;
each step class:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
???????
as you can see the "?????" part is what I am trying to understand - How can I convert the items without needing to know the field name..? meaning to a HashMap/another iterable object..? can I initialize using a method..?
I have tried creating an Item class with a constructor but I do not understand how to use it in this case..
java json serialization initialization gson
actually gson is pretty simple to use, you dont need to explicity give the serialization name.... @SerializedName("items")
– ΦXocę 웃 Пepeúpa ツ
Nov 6 '17 at 16:53
So what do I do instead...? How can I convert this file to this class without it...?
– Dardar1991
Nov 6 '17 at 16:58
add a comment |
I am very new to java and just started with json...
I have the following json file:
"step1":
"version": 1,
"items":
"run": false,
"jump": true
,
"step2":
"version": "None",
"items":
"happy": true,
"sad": false
I am using Gson in my main like this:
Gson gs = new Gson();
Content tmp = gs.fromJson(<json string>, Content.class);
my class:
public class Content
@SerializedName("step1")
private Step step1;
@SerializedName("step2")
private Step step2;
each step class:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
???????
as you can see the "?????" part is what I am trying to understand - How can I convert the items without needing to know the field name..? meaning to a HashMap/another iterable object..? can I initialize using a method..?
I have tried creating an Item class with a constructor but I do not understand how to use it in this case..
java json serialization initialization gson
I am very new to java and just started with json...
I have the following json file:
"step1":
"version": 1,
"items":
"run": false,
"jump": true
,
"step2":
"version": "None",
"items":
"happy": true,
"sad": false
I am using Gson in my main like this:
Gson gs = new Gson();
Content tmp = gs.fromJson(<json string>, Content.class);
my class:
public class Content
@SerializedName("step1")
private Step step1;
@SerializedName("step2")
private Step step2;
each step class:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
???????
as you can see the "?????" part is what I am trying to understand - How can I convert the items without needing to know the field name..? meaning to a HashMap/another iterable object..? can I initialize using a method..?
I have tried creating an Item class with a constructor but I do not understand how to use it in this case..
java json serialization initialization gson
java json serialization initialization gson
edited Mar 26 at 3:56
Cœur
21.5k10 gold badges123 silver badges169 bronze badges
21.5k10 gold badges123 silver badges169 bronze badges
asked Nov 6 '17 at 16:51
Dardar1991Dardar1991
279 bronze badges
279 bronze badges
actually gson is pretty simple to use, you dont need to explicity give the serialization name.... @SerializedName("items")
– ΦXocę 웃 Пepeúpa ツ
Nov 6 '17 at 16:53
So what do I do instead...? How can I convert this file to this class without it...?
– Dardar1991
Nov 6 '17 at 16:58
add a comment |
actually gson is pretty simple to use, you dont need to explicity give the serialization name.... @SerializedName("items")
– ΦXocę 웃 Пepeúpa ツ
Nov 6 '17 at 16:53
So what do I do instead...? How can I convert this file to this class without it...?
– Dardar1991
Nov 6 '17 at 16:58
actually gson is pretty simple to use, you dont need to explicity give the serialization name.... @SerializedName("items")
– ΦXocę 웃 Пepeúpa ツ
Nov 6 '17 at 16:53
actually gson is pretty simple to use, you dont need to explicity give the serialization name.... @SerializedName("items")
– ΦXocę 웃 Пepeúpa ツ
Nov 6 '17 at 16:53
So what do I do instead...? How can I convert this file to this class without it...?
– Dardar1991
Nov 6 '17 at 16:58
So what do I do instead...? How can I convert this file to this class without it...?
– Dardar1991
Nov 6 '17 at 16:58
add a comment |
2 Answers
2
active
oldest
votes
The solution that worked for me is:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
private HashMap<String,Boolean> items;
add a comment |
You can use a Map<String, Boolean>
to store your items. You Step
class can be something, like this:
public class Step
private String version;
private Map<String, Boolean> items;
Then you can add your values to the Map:
Step step = new Step();
step.setVersion("None");
Map<String, Boolean> items = new HashMap<>();
items.put("happy", Boolean.TRUE);
items.put("sad", Boolean.FALSE);
step.setItems(items);
Hope that helps you.
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%2f47141927%2fgenerate-json-to-a-unique-custom-class%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
The solution that worked for me is:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
private HashMap<String,Boolean> items;
add a comment |
The solution that worked for me is:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
private HashMap<String,Boolean> items;
add a comment |
The solution that worked for me is:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
private HashMap<String,Boolean> items;
The solution that worked for me is:
public class Step
@SerializedName("version")
private String version;
@SerializedName("items")
private HashMap<String,Boolean> items;
answered Nov 7 '17 at 11:44
Dardar1991Dardar1991
279 bronze badges
279 bronze badges
add a comment |
add a comment |
You can use a Map<String, Boolean>
to store your items. You Step
class can be something, like this:
public class Step
private String version;
private Map<String, Boolean> items;
Then you can add your values to the Map:
Step step = new Step();
step.setVersion("None");
Map<String, Boolean> items = new HashMap<>();
items.put("happy", Boolean.TRUE);
items.put("sad", Boolean.FALSE);
step.setItems(items);
Hope that helps you.
add a comment |
You can use a Map<String, Boolean>
to store your items. You Step
class can be something, like this:
public class Step
private String version;
private Map<String, Boolean> items;
Then you can add your values to the Map:
Step step = new Step();
step.setVersion("None");
Map<String, Boolean> items = new HashMap<>();
items.put("happy", Boolean.TRUE);
items.put("sad", Boolean.FALSE);
step.setItems(items);
Hope that helps you.
add a comment |
You can use a Map<String, Boolean>
to store your items. You Step
class can be something, like this:
public class Step
private String version;
private Map<String, Boolean> items;
Then you can add your values to the Map:
Step step = new Step();
step.setVersion("None");
Map<String, Boolean> items = new HashMap<>();
items.put("happy", Boolean.TRUE);
items.put("sad", Boolean.FALSE);
step.setItems(items);
Hope that helps you.
You can use a Map<String, Boolean>
to store your items. You Step
class can be something, like this:
public class Step
private String version;
private Map<String, Boolean> items;
Then you can add your values to the Map:
Step step = new Step();
step.setVersion("None");
Map<String, Boolean> items = new HashMap<>();
items.put("happy", Boolean.TRUE);
items.put("sad", Boolean.FALSE);
step.setItems(items);
Hope that helps you.
edited Nov 7 '17 at 12:29
answered Nov 6 '17 at 17:00
zappeezappee
3,6905 gold badges39 silver badges59 bronze badges
3,6905 gold badges39 silver badges59 bronze badges
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%2f47141927%2fgenerate-json-to-a-unique-custom-class%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
actually gson is pretty simple to use, you dont need to explicity give the serialization name.... @SerializedName("items")
– ΦXocę 웃 Пepeúpa ツ
Nov 6 '17 at 16:53
So what do I do instead...? How can I convert this file to this class without it...?
– Dardar1991
Nov 6 '17 at 16:58