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;








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..










share|improve this question
























  • 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

















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..










share|improve this question
























  • 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













0












0








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..










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












2 Answers
2






active

oldest

votes


















0














The solution that worked for me is:



public class Step
@SerializedName("version")
private String version;

@SerializedName("items")
private HashMap<String,Boolean> items;






share|improve this answer






























    0














    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.






    share|improve this answer



























      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%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









      0














      The solution that worked for me is:



      public class Step
      @SerializedName("version")
      private String version;

      @SerializedName("items")
      private HashMap<String,Boolean> items;






      share|improve this answer



























        0














        The solution that worked for me is:



        public class Step
        @SerializedName("version")
        private String version;

        @SerializedName("items")
        private HashMap<String,Boolean> items;






        share|improve this answer

























          0












          0








          0







          The solution that worked for me is:



          public class Step
          @SerializedName("version")
          private String version;

          @SerializedName("items")
          private HashMap<String,Boolean> items;






          share|improve this answer













          The solution that worked for me is:



          public class Step
          @SerializedName("version")
          private String version;

          @SerializedName("items")
          private HashMap<String,Boolean> items;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 '17 at 11:44









          Dardar1991Dardar1991

          279 bronze badges




          279 bronze badges























              0














              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.






              share|improve this answer





























                0














                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.






                share|improve this answer



























                  0












                  0








                  0







                  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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  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



























                      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%2f47141927%2fgenerate-json-to-a-unique-custom-class%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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript