Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?

How to know if a folder is a symbolic link?

Looking for a soft substance that doesn't dissolve underwater

Is there a way to make it so the cursor is included when I prtscr key?

If a person had control of every single cell of their body, would they be able to transform into another creature?

Why do most published works in medical imaging try to reduce false positives?

Why do Ryanair allow me to book connecting itineraries through a third party, but not through their own website?

Does Nitrogen inside commercial airliner wheels prevent blowouts on touchdown?

Are these reasonable traits for someone with autism?

How to make a villain fall in love?

Would jet fuel for an F-16 or F-35 be producible during WW2?

Is there an efficient way to replace text matching the entire content of one file with the entire content of another file?

Using credit/debit card details vs swiping a card in a payment (credit card) terminal

Should I disclose a colleague's illness (that I should not know) when others badmouth him

Is it possible to play as a necromancer skeleton?

Which is the common name of Mind Flayers?

Why do compressed liquids heat up when allowed to expand unlike gases?

Defining the standard model of PA so that a space alien could understand

how to grep in the output of ls -a

Why did David Cameron offer a referendum on the European Union?

In general, would I need to season a meat when making a sauce?

Passing arguments from TeX to a Lua function

What are these arcade games in Ghostbusters 1984?

At what point in European history could a government build a printing press given a basic description?

I think I may have violated academic integrity last year - what should I do?



Convert JSON to Map


Simplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








165















What is the best way to convert a JSON code as this:



 
"data" :

"field1" : "value1",
"field2" : "value2"




in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2).



Any ideas? Should I use Json-lib for that? Or better if I write my own parser?










share|improve this question
























  • I have written code for this without using any library. stackoverflow.com/questions/21720759/jsonobject-to-map/…

    – Vikas Gupta
    Oct 31 '14 at 9:43


















165















What is the best way to convert a JSON code as this:



 
"data" :

"field1" : "value1",
"field2" : "value2"




in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2).



Any ideas? Should I use Json-lib for that? Or better if I write my own parser?










share|improve this question
























  • I have written code for this without using any library. stackoverflow.com/questions/21720759/jsonobject-to-map/…

    – Vikas Gupta
    Oct 31 '14 at 9:43














165












165








165


45






What is the best way to convert a JSON code as this:



 
"data" :

"field1" : "value1",
"field2" : "value2"




in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2).



Any ideas? Should I use Json-lib for that? Or better if I write my own parser?










share|improve this question
















What is the best way to convert a JSON code as this:



 
"data" :

"field1" : "value1",
"field2" : "value2"




in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2).



Any ideas? Should I use Json-lib for that? Or better if I write my own parser?







java json parsing collections






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '17 at 22:18









Donald Duck

4,142134263




4,142134263










asked Jan 14 '09 at 16:00









David SantamariaDavid Santamaria

5,23062942




5,23062942












  • I have written code for this without using any library. stackoverflow.com/questions/21720759/jsonobject-to-map/…

    – Vikas Gupta
    Oct 31 '14 at 9:43


















  • I have written code for this without using any library. stackoverflow.com/questions/21720759/jsonobject-to-map/…

    – Vikas Gupta
    Oct 31 '14 at 9:43

















I have written code for this without using any library. stackoverflow.com/questions/21720759/jsonobject-to-map/…

– Vikas Gupta
Oct 31 '14 at 9:43






I have written code for this without using any library. stackoverflow.com/questions/21720759/jsonobject-to-map/…

– Vikas Gupta
Oct 31 '14 at 9:43













14 Answers
14






active

oldest

votes


















301














I hope you were joking about writing your own parser. :-)



For such a simple mapping, most tools from http://json.org (section java) would work.
For one of them (Jackson, http://wiki.fasterxml.com/JacksonInFiveMinutes), you'd do:



HashMap<String,Object> result =
new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);


(where JSON_SOURCE is a File, input stream, reader, or json content String)






share|improve this answer




















  • 32





    Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

    – obe6
    Dec 29 '14 at 20:46






  • 3





    If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

    – LoBo
    Nov 2 '15 at 9:49







  • 1





    And looks like Jackson has moved since 09 so: Jackson data-bind

    – jacob.mccrumb
    Sep 21 '16 at 18:42











  • @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

    – Aetherus
    Nov 22 '18 at 9:39











  • FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

    – StaxMan
    Nov 28 '18 at 6:40



















30














I like google gson library.

When you don't know structure of json. You can use



JsonElement root = new JsonParser().parse(jsonString);


and then you can work with json. e.g. how to get "value1" from your gson:



String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();





share|improve this answer


















  • 14





    This can throw NullPointerExceptions 5 different ways if you get unexpected data.

    – dfraser
    Aug 27 '15 at 14:31











  • i hate the google gson library, it's rubbish

    – bharal
    Jan 9 at 18:22


















29














Using the GSON library:



import com.google.gson.Gson;
import com.google.common.reflect.TypeToken;
import java.lang.reclect.Type;


Use the following code:



Type mapType = new TypeToken<Map<String, Map>>().getType(); 
Map<String, String[]> son = new Gson().fromJson(easyString, mapType);





share|improve this answer




















  • 5





    java at its best: TypeToken<Map<String, Map>> :-)

    – froderik
    Oct 26 '14 at 8:10











  • With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

    – 9ilsdx 9rvj 0lo
    Jan 10 '18 at 13:19


















16














Use JSON lib E.g. http://www.json.org/java/



// Assume you have a Map<String, String> in JSONObject jdata
@SuppressWarnings("unchecked")
Iterator<String> nameItr = jdata.keys();
Map<String, String> outMap = new HashMap<String, String>();
while(nameItr.hasNext())
String name = nameItr.next();
outMap.put(name, jdata.getString(name));







share|improve this answer
































    12














    My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:



     
    "shopping_list":
    "996386":
    "id":996386,
    "label":"My 1st shopping list",
    "current":true,
    "nb_reference":6
    ,
    "888540":
    "id":888540,
    "label":"My 2nd shopping list",
    "current":false,
    "nb_reference":2





    To parse this JSON file with GSON library, it's easy :
    if your project is mavenized



    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
    </dependency>


    Then use this snippet :



    import com.google.gson.Gson;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;

    //Read the JSON file
    JsonElement root = new JsonParser().parse(new FileReader("/path/to/the/json/file/in/your/file/system.json"));

    //Get the content of the first map
    JsonObject object = root.getAsJsonObject().get("shopping_list").getAsJsonObject();

    //Iterate over this map
    Gson gson = new Gson();
    for (Entry<String, JsonElement> entry : object.entrySet())
    ShoppingList shoppingList = gson.fromJson(entry.getValue(), ShoppingList.class);
    System.out.println(shoppingList.getLabel());



    The corresponding POJO should be something like that :



    public class ShoppingList 

    int id;

    String label;

    boolean current;

    int nb_reference;

    //Setters & Getters !!!!!



    Hope it helps !






    share|improve this answer






























      5














      This way its works like a Map...



      JSONObject fieldsJson = new JSONObject(json);
      String value = fieldsJson.getString(key);

      <dependency>
      <groupId>org.codehaus.jettison</groupId>
      <artifactId>jettison</artifactId>
      <version>1.1</version>
      </dependency>





      share|improve this answer




















      • 1





        Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

        – Maciej Swic
        Oct 28 '13 at 9:28







      • 1





        you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

        – Jeffrey Blattman
        Jan 16 '14 at 18:54











      • Please add used libraries.

        – mvermand
        Jan 22 '18 at 6:56






      • 1





        @mvermand Added the dependency I've used back then

        – Fabio Araujo
        Jan 23 '18 at 11:02


















      4














      I do it this way. It's Simple.



      import java.util.Map;
      import org.json.JSONObject;
      import com.google.gson.Gson;

      public class Main
      public static void main(String[] args)
      JSONObject jsonObj = new JSONObject(" "f1":"v1"");
      @SuppressWarnings("unchecked")
      Map<String, String> map = new Gson().fromJson(jsonObj.toString(),Map.class);
      System.out.println(map);







      share|improve this answer
































        3














        java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>().getType();
        Gson gson = new Gson();
        Map<String, Object> categoryicons = gson.fromJson(json, mapType );





        share|improve this answer






























          3














          The JsonTools library is very complete. It can be found at Github.






          share|improve this answer

























          • @zygimantus, corrected the link, added new location.

            – Bruno Ranschaert
            Oct 6 '16 at 15:22


















          2














          With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:



          Map map = gson.fromJson(json, Map.class);


          Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.






          share|improve this answer






























            1














            One more alternative is json-simple which can be found in Maven Central:



            (JSONObject)JSONValue.parse(someString); //JSONObject is actually a Map.


            The artifact is 24kbytes, doesn't have other runtime dependencies.






            share|improve this answer























            • I get null back from a simple JSON string where one of the values is an array of strings.

              – isapir
              Sep 12 '16 at 5:45


















            0














            import net.sf.json.JSONObject

            JSONObject.fromObject(yourJsonString).toMap





            share|improve this answer






























              0














              Underscore-java library can convert json string to hash map. I am the maintainer of the project.



              Code example:



              import com.github.underscore.lodash.U;
              import java.util.*;

              public class Main

              @SuppressWarnings("unchecked")
              public static void main(String[] args)
              String json = ""
              + " "data" :"
              + " "
              + " "field1" : "value1","
              + " "field2" : "value2""
              + " "
              + "";

              Map<String, Object> data = (Map) U.get((Map<String, Object>) U.fromJson(json), "data");
              System.out.println(data);

              // field1=value1, field2=value2







              share|improve this answer
































                -1














                JSON to Map always gonna be a string/object data type. i haved GSON lib from google.



                works very well and JDK 1.5 is the min requirement.






                share|improve this answer


















                • 1





                  How did you use GSON?

                  – NinjaCoder
                  Jun 8 '15 at 20:08











                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%2f443499%2fconvert-json-to-map%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                14 Answers
                14






                active

                oldest

                votes








                14 Answers
                14






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                301














                I hope you were joking about writing your own parser. :-)



                For such a simple mapping, most tools from http://json.org (section java) would work.
                For one of them (Jackson, http://wiki.fasterxml.com/JacksonInFiveMinutes), you'd do:



                HashMap<String,Object> result =
                new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);


                (where JSON_SOURCE is a File, input stream, reader, or json content String)






                share|improve this answer




















                • 32





                  Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

                  – obe6
                  Dec 29 '14 at 20:46






                • 3





                  If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

                  – LoBo
                  Nov 2 '15 at 9:49







                • 1





                  And looks like Jackson has moved since 09 so: Jackson data-bind

                  – jacob.mccrumb
                  Sep 21 '16 at 18:42











                • @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

                  – Aetherus
                  Nov 22 '18 at 9:39











                • FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

                  – StaxMan
                  Nov 28 '18 at 6:40
















                301














                I hope you were joking about writing your own parser. :-)



                For such a simple mapping, most tools from http://json.org (section java) would work.
                For one of them (Jackson, http://wiki.fasterxml.com/JacksonInFiveMinutes), you'd do:



                HashMap<String,Object> result =
                new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);


                (where JSON_SOURCE is a File, input stream, reader, or json content String)






                share|improve this answer




















                • 32





                  Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

                  – obe6
                  Dec 29 '14 at 20:46






                • 3





                  If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

                  – LoBo
                  Nov 2 '15 at 9:49







                • 1





                  And looks like Jackson has moved since 09 so: Jackson data-bind

                  – jacob.mccrumb
                  Sep 21 '16 at 18:42











                • @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

                  – Aetherus
                  Nov 22 '18 at 9:39











                • FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

                  – StaxMan
                  Nov 28 '18 at 6:40














                301












                301








                301







                I hope you were joking about writing your own parser. :-)



                For such a simple mapping, most tools from http://json.org (section java) would work.
                For one of them (Jackson, http://wiki.fasterxml.com/JacksonInFiveMinutes), you'd do:



                HashMap<String,Object> result =
                new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);


                (where JSON_SOURCE is a File, input stream, reader, or json content String)






                share|improve this answer















                I hope you were joking about writing your own parser. :-)



                For such a simple mapping, most tools from http://json.org (section java) would work.
                For one of them (Jackson, http://wiki.fasterxml.com/JacksonInFiveMinutes), you'd do:



                HashMap<String,Object> result =
                new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);


                (where JSON_SOURCE is a File, input stream, reader, or json content String)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 8 '12 at 0:54









                jedwards

                22.1k13667




                22.1k13667










                answered Feb 24 '09 at 21:11









                StaxManStaxMan

                84.5k26167205




                84.5k26167205







                • 32





                  Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

                  – obe6
                  Dec 29 '14 at 20:46






                • 3





                  If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

                  – LoBo
                  Nov 2 '15 at 9:49







                • 1





                  And looks like Jackson has moved since 09 so: Jackson data-bind

                  – jacob.mccrumb
                  Sep 21 '16 at 18:42











                • @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

                  – Aetherus
                  Nov 22 '18 at 9:39











                • FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

                  – StaxMan
                  Nov 28 '18 at 6:40













                • 32





                  Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

                  – obe6
                  Dec 29 '14 at 20:46






                • 3





                  If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

                  – LoBo
                  Nov 2 '15 at 9:49







                • 1





                  And looks like Jackson has moved since 09 so: Jackson data-bind

                  – jacob.mccrumb
                  Sep 21 '16 at 18:42











                • @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

                  – Aetherus
                  Nov 22 '18 at 9:39











                • FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

                  – StaxMan
                  Nov 28 '18 at 6:40








                32




                32





                Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

                – obe6
                Dec 29 '14 at 20:46





                Moreover, if you want a typed Map (exploiting java generics), you can do : Map<String, MyPojo> typedMap = mapper.readValue(jsonStream, new TypeReference<Map<String, MyPojo>>() );

                – obe6
                Dec 29 '14 at 20:46




                3




                3





                If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

                – LoBo
                Nov 2 '15 at 9:49






                If you work with Maven project, you will need <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency>

                – LoBo
                Nov 2 '15 at 9:49





                1




                1





                And looks like Jackson has moved since 09 so: Jackson data-bind

                – jacob.mccrumb
                Sep 21 '16 at 18:42





                And looks like Jackson has moved since 09 so: Jackson data-bind

                – jacob.mccrumb
                Sep 21 '16 at 18:42













                @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

                – Aetherus
                Nov 22 '18 at 9:39





                @obe6 I guess the OP is not joking. Can you find a single "JSON" anywhere in your code snippet other than that arbitrary variable name? I'm struggling too because of the names in Jackson API.

                – Aetherus
                Nov 22 '18 at 9:39













                FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

                – StaxMan
                Nov 28 '18 at 6:40






                FWTW, while @obe6's answer is technically correct, you can use shorter form too: Map<String,Object> result = mapper.readValue(source, Map.class).

                – StaxMan
                Nov 28 '18 at 6:40














                30














                I like google gson library.

                When you don't know structure of json. You can use



                JsonElement root = new JsonParser().parse(jsonString);


                and then you can work with json. e.g. how to get "value1" from your gson:



                String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();





                share|improve this answer


















                • 14





                  This can throw NullPointerExceptions 5 different ways if you get unexpected data.

                  – dfraser
                  Aug 27 '15 at 14:31











                • i hate the google gson library, it's rubbish

                  – bharal
                  Jan 9 at 18:22















                30














                I like google gson library.

                When you don't know structure of json. You can use



                JsonElement root = new JsonParser().parse(jsonString);


                and then you can work with json. e.g. how to get "value1" from your gson:



                String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();





                share|improve this answer


















                • 14





                  This can throw NullPointerExceptions 5 different ways if you get unexpected data.

                  – dfraser
                  Aug 27 '15 at 14:31











                • i hate the google gson library, it's rubbish

                  – bharal
                  Jan 9 at 18:22













                30












                30








                30







                I like google gson library.

                When you don't know structure of json. You can use



                JsonElement root = new JsonParser().parse(jsonString);


                and then you can work with json. e.g. how to get "value1" from your gson:



                String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();





                share|improve this answer













                I like google gson library.

                When you don't know structure of json. You can use



                JsonElement root = new JsonParser().parse(jsonString);


                and then you can work with json. e.g. how to get "value1" from your gson:



                String value1 = root.getAsJsonObject().get("data").getAsJsonObject().get("field1").getAsString();






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 1 '11 at 10:15









                bugs_bugs_

                2,43432233




                2,43432233







                • 14





                  This can throw NullPointerExceptions 5 different ways if you get unexpected data.

                  – dfraser
                  Aug 27 '15 at 14:31











                • i hate the google gson library, it's rubbish

                  – bharal
                  Jan 9 at 18:22












                • 14





                  This can throw NullPointerExceptions 5 different ways if you get unexpected data.

                  – dfraser
                  Aug 27 '15 at 14:31











                • i hate the google gson library, it's rubbish

                  – bharal
                  Jan 9 at 18:22







                14




                14





                This can throw NullPointerExceptions 5 different ways if you get unexpected data.

                – dfraser
                Aug 27 '15 at 14:31





                This can throw NullPointerExceptions 5 different ways if you get unexpected data.

                – dfraser
                Aug 27 '15 at 14:31













                i hate the google gson library, it's rubbish

                – bharal
                Jan 9 at 18:22





                i hate the google gson library, it's rubbish

                – bharal
                Jan 9 at 18:22











                29














                Using the GSON library:



                import com.google.gson.Gson;
                import com.google.common.reflect.TypeToken;
                import java.lang.reclect.Type;


                Use the following code:



                Type mapType = new TypeToken<Map<String, Map>>().getType(); 
                Map<String, String[]> son = new Gson().fromJson(easyString, mapType);





                share|improve this answer




















                • 5





                  java at its best: TypeToken<Map<String, Map>> :-)

                  – froderik
                  Oct 26 '14 at 8:10











                • With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

                  – 9ilsdx 9rvj 0lo
                  Jan 10 '18 at 13:19















                29














                Using the GSON library:



                import com.google.gson.Gson;
                import com.google.common.reflect.TypeToken;
                import java.lang.reclect.Type;


                Use the following code:



                Type mapType = new TypeToken<Map<String, Map>>().getType(); 
                Map<String, String[]> son = new Gson().fromJson(easyString, mapType);





                share|improve this answer




















                • 5





                  java at its best: TypeToken<Map<String, Map>> :-)

                  – froderik
                  Oct 26 '14 at 8:10











                • With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

                  – 9ilsdx 9rvj 0lo
                  Jan 10 '18 at 13:19













                29












                29








                29







                Using the GSON library:



                import com.google.gson.Gson;
                import com.google.common.reflect.TypeToken;
                import java.lang.reclect.Type;


                Use the following code:



                Type mapType = new TypeToken<Map<String, Map>>().getType(); 
                Map<String, String[]> son = new Gson().fromJson(easyString, mapType);





                share|improve this answer















                Using the GSON library:



                import com.google.gson.Gson;
                import com.google.common.reflect.TypeToken;
                import java.lang.reclect.Type;


                Use the following code:



                Type mapType = new TypeToken<Map<String, Map>>().getType(); 
                Map<String, String[]> son = new Gson().fromJson(easyString, mapType);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 14 at 13:46









                user1170330

                6,0852367111




                6,0852367111










                answered Nov 15 '11 at 6:25









                David LDavid L

                30732




                30732







                • 5





                  java at its best: TypeToken<Map<String, Map>> :-)

                  – froderik
                  Oct 26 '14 at 8:10











                • With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

                  – 9ilsdx 9rvj 0lo
                  Jan 10 '18 at 13:19












                • 5





                  java at its best: TypeToken<Map<String, Map>> :-)

                  – froderik
                  Oct 26 '14 at 8:10











                • With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

                  – 9ilsdx 9rvj 0lo
                  Jan 10 '18 at 13:19







                5




                5





                java at its best: TypeToken<Map<String, Map>> :-)

                – froderik
                Oct 26 '14 at 8:10





                java at its best: TypeToken<Map<String, Map>> :-)

                – froderik
                Oct 26 '14 at 8:10













                With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

                – 9ilsdx 9rvj 0lo
                Jan 10 '18 at 13:19





                With new TypeToken<Map<String, Object>> it will work also when there are arrays inside.

                – 9ilsdx 9rvj 0lo
                Jan 10 '18 at 13:19











                16














                Use JSON lib E.g. http://www.json.org/java/



                // Assume you have a Map<String, String> in JSONObject jdata
                @SuppressWarnings("unchecked")
                Iterator<String> nameItr = jdata.keys();
                Map<String, String> outMap = new HashMap<String, String>();
                while(nameItr.hasNext())
                String name = nameItr.next();
                outMap.put(name, jdata.getString(name));







                share|improve this answer





























                  16














                  Use JSON lib E.g. http://www.json.org/java/



                  // Assume you have a Map<String, String> in JSONObject jdata
                  @SuppressWarnings("unchecked")
                  Iterator<String> nameItr = jdata.keys();
                  Map<String, String> outMap = new HashMap<String, String>();
                  while(nameItr.hasNext())
                  String name = nameItr.next();
                  outMap.put(name, jdata.getString(name));







                  share|improve this answer



























                    16












                    16








                    16







                    Use JSON lib E.g. http://www.json.org/java/



                    // Assume you have a Map<String, String> in JSONObject jdata
                    @SuppressWarnings("unchecked")
                    Iterator<String> nameItr = jdata.keys();
                    Map<String, String> outMap = new HashMap<String, String>();
                    while(nameItr.hasNext())
                    String name = nameItr.next();
                    outMap.put(name, jdata.getString(name));







                    share|improve this answer















                    Use JSON lib E.g. http://www.json.org/java/



                    // Assume you have a Map<String, String> in JSONObject jdata
                    @SuppressWarnings("unchecked")
                    Iterator<String> nameItr = jdata.keys();
                    Map<String, String> outMap = new HashMap<String, String>();
                    while(nameItr.hasNext())
                    String name = nameItr.next();
                    outMap.put(name, jdata.getString(name));








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 14 '15 at 16:58









                    El Marce

                    1,61911432




                    1,61911432










                    answered Nov 10 '10 at 22:02









                    j.d.j.d.

                    16112




                    16112





















                        12














                        My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:



                         
                        "shopping_list":
                        "996386":
                        "id":996386,
                        "label":"My 1st shopping list",
                        "current":true,
                        "nb_reference":6
                        ,
                        "888540":
                        "id":888540,
                        "label":"My 2nd shopping list",
                        "current":false,
                        "nb_reference":2





                        To parse this JSON file with GSON library, it's easy :
                        if your project is mavenized



                        <dependency>
                        <groupId>com.google.code.gson</groupId>
                        <artifactId>gson</artifactId>
                        <version>2.3.1</version>
                        </dependency>


                        Then use this snippet :



                        import com.google.gson.Gson;
                        import com.google.gson.JsonElement;
                        import com.google.gson.JsonObject;
                        import com.google.gson.JsonParser;

                        //Read the JSON file
                        JsonElement root = new JsonParser().parse(new FileReader("/path/to/the/json/file/in/your/file/system.json"));

                        //Get the content of the first map
                        JsonObject object = root.getAsJsonObject().get("shopping_list").getAsJsonObject();

                        //Iterate over this map
                        Gson gson = new Gson();
                        for (Entry<String, JsonElement> entry : object.entrySet())
                        ShoppingList shoppingList = gson.fromJson(entry.getValue(), ShoppingList.class);
                        System.out.println(shoppingList.getLabel());



                        The corresponding POJO should be something like that :



                        public class ShoppingList 

                        int id;

                        String label;

                        boolean current;

                        int nb_reference;

                        //Setters & Getters !!!!!



                        Hope it helps !






                        share|improve this answer



























                          12














                          My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:



                           
                          "shopping_list":
                          "996386":
                          "id":996386,
                          "label":"My 1st shopping list",
                          "current":true,
                          "nb_reference":6
                          ,
                          "888540":
                          "id":888540,
                          "label":"My 2nd shopping list",
                          "current":false,
                          "nb_reference":2





                          To parse this JSON file with GSON library, it's easy :
                          if your project is mavenized



                          <dependency>
                          <groupId>com.google.code.gson</groupId>
                          <artifactId>gson</artifactId>
                          <version>2.3.1</version>
                          </dependency>


                          Then use this snippet :



                          import com.google.gson.Gson;
                          import com.google.gson.JsonElement;
                          import com.google.gson.JsonObject;
                          import com.google.gson.JsonParser;

                          //Read the JSON file
                          JsonElement root = new JsonParser().parse(new FileReader("/path/to/the/json/file/in/your/file/system.json"));

                          //Get the content of the first map
                          JsonObject object = root.getAsJsonObject().get("shopping_list").getAsJsonObject();

                          //Iterate over this map
                          Gson gson = new Gson();
                          for (Entry<String, JsonElement> entry : object.entrySet())
                          ShoppingList shoppingList = gson.fromJson(entry.getValue(), ShoppingList.class);
                          System.out.println(shoppingList.getLabel());



                          The corresponding POJO should be something like that :



                          public class ShoppingList 

                          int id;

                          String label;

                          boolean current;

                          int nb_reference;

                          //Setters & Getters !!!!!



                          Hope it helps !






                          share|improve this answer

























                            12












                            12








                            12







                            My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:



                             
                            "shopping_list":
                            "996386":
                            "id":996386,
                            "label":"My 1st shopping list",
                            "current":true,
                            "nb_reference":6
                            ,
                            "888540":
                            "id":888540,
                            "label":"My 2nd shopping list",
                            "current":false,
                            "nb_reference":2





                            To parse this JSON file with GSON library, it's easy :
                            if your project is mavenized



                            <dependency>
                            <groupId>com.google.code.gson</groupId>
                            <artifactId>gson</artifactId>
                            <version>2.3.1</version>
                            </dependency>


                            Then use this snippet :



                            import com.google.gson.Gson;
                            import com.google.gson.JsonElement;
                            import com.google.gson.JsonObject;
                            import com.google.gson.JsonParser;

                            //Read the JSON file
                            JsonElement root = new JsonParser().parse(new FileReader("/path/to/the/json/file/in/your/file/system.json"));

                            //Get the content of the first map
                            JsonObject object = root.getAsJsonObject().get("shopping_list").getAsJsonObject();

                            //Iterate over this map
                            Gson gson = new Gson();
                            for (Entry<String, JsonElement> entry : object.entrySet())
                            ShoppingList shoppingList = gson.fromJson(entry.getValue(), ShoppingList.class);
                            System.out.println(shoppingList.getLabel());



                            The corresponding POJO should be something like that :



                            public class ShoppingList 

                            int id;

                            String label;

                            boolean current;

                            int nb_reference;

                            //Setters & Getters !!!!!



                            Hope it helps !






                            share|improve this answer













                            My post could be helpful for others, so imagine you have a map with a specific object in values, something like that:



                             
                            "shopping_list":
                            "996386":
                            "id":996386,
                            "label":"My 1st shopping list",
                            "current":true,
                            "nb_reference":6
                            ,
                            "888540":
                            "id":888540,
                            "label":"My 2nd shopping list",
                            "current":false,
                            "nb_reference":2





                            To parse this JSON file with GSON library, it's easy :
                            if your project is mavenized



                            <dependency>
                            <groupId>com.google.code.gson</groupId>
                            <artifactId>gson</artifactId>
                            <version>2.3.1</version>
                            </dependency>


                            Then use this snippet :



                            import com.google.gson.Gson;
                            import com.google.gson.JsonElement;
                            import com.google.gson.JsonObject;
                            import com.google.gson.JsonParser;

                            //Read the JSON file
                            JsonElement root = new JsonParser().parse(new FileReader("/path/to/the/json/file/in/your/file/system.json"));

                            //Get the content of the first map
                            JsonObject object = root.getAsJsonObject().get("shopping_list").getAsJsonObject();

                            //Iterate over this map
                            Gson gson = new Gson();
                            for (Entry<String, JsonElement> entry : object.entrySet())
                            ShoppingList shoppingList = gson.fromJson(entry.getValue(), ShoppingList.class);
                            System.out.println(shoppingList.getLabel());



                            The corresponding POJO should be something like that :



                            public class ShoppingList 

                            int id;

                            String label;

                            boolean current;

                            int nb_reference;

                            //Setters & Getters !!!!!



                            Hope it helps !







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 18 '15 at 7:46









                            Jad B.Jad B.

                            1,1271214




                            1,1271214





















                                5














                                This way its works like a Map...



                                JSONObject fieldsJson = new JSONObject(json);
                                String value = fieldsJson.getString(key);

                                <dependency>
                                <groupId>org.codehaus.jettison</groupId>
                                <artifactId>jettison</artifactId>
                                <version>1.1</version>
                                </dependency>





                                share|improve this answer




















                                • 1





                                  Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

                                  – Maciej Swic
                                  Oct 28 '13 at 9:28







                                • 1





                                  you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

                                  – Jeffrey Blattman
                                  Jan 16 '14 at 18:54











                                • Please add used libraries.

                                  – mvermand
                                  Jan 22 '18 at 6:56






                                • 1





                                  @mvermand Added the dependency I've used back then

                                  – Fabio Araujo
                                  Jan 23 '18 at 11:02















                                5














                                This way its works like a Map...



                                JSONObject fieldsJson = new JSONObject(json);
                                String value = fieldsJson.getString(key);

                                <dependency>
                                <groupId>org.codehaus.jettison</groupId>
                                <artifactId>jettison</artifactId>
                                <version>1.1</version>
                                </dependency>





                                share|improve this answer




















                                • 1





                                  Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

                                  – Maciej Swic
                                  Oct 28 '13 at 9:28







                                • 1





                                  you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

                                  – Jeffrey Blattman
                                  Jan 16 '14 at 18:54











                                • Please add used libraries.

                                  – mvermand
                                  Jan 22 '18 at 6:56






                                • 1





                                  @mvermand Added the dependency I've used back then

                                  – Fabio Araujo
                                  Jan 23 '18 at 11:02













                                5












                                5








                                5







                                This way its works like a Map...



                                JSONObject fieldsJson = new JSONObject(json);
                                String value = fieldsJson.getString(key);

                                <dependency>
                                <groupId>org.codehaus.jettison</groupId>
                                <artifactId>jettison</artifactId>
                                <version>1.1</version>
                                </dependency>





                                share|improve this answer















                                This way its works like a Map...



                                JSONObject fieldsJson = new JSONObject(json);
                                String value = fieldsJson.getString(key);

                                <dependency>
                                <groupId>org.codehaus.jettison</groupId>
                                <artifactId>jettison</artifactId>
                                <version>1.1</version>
                                </dependency>






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Jan 23 '18 at 11:00

























                                answered Jan 4 '12 at 11:22









                                Fabio AraujoFabio Araujo

                                11127




                                11127







                                • 1





                                  Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

                                  – Maciej Swic
                                  Oct 28 '13 at 9:28







                                • 1





                                  you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

                                  – Jeffrey Blattman
                                  Jan 16 '14 at 18:54











                                • Please add used libraries.

                                  – mvermand
                                  Jan 22 '18 at 6:56






                                • 1





                                  @mvermand Added the dependency I've used back then

                                  – Fabio Araujo
                                  Jan 23 '18 at 11:02












                                • 1





                                  Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

                                  – Maciej Swic
                                  Oct 28 '13 at 9:28







                                • 1





                                  you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

                                  – Jeffrey Blattman
                                  Jan 16 '14 at 18:54











                                • Please add used libraries.

                                  – mvermand
                                  Jan 22 '18 at 6:56






                                • 1





                                  @mvermand Added the dependency I've used back then

                                  – Fabio Araujo
                                  Jan 23 '18 at 11:02







                                1




                                1





                                Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

                                – Maciej Swic
                                Oct 28 '13 at 9:28






                                Yes, but you must wrap every single ´get´ in try/catch because of JSON exceptions.

                                – Maciej Swic
                                Oct 28 '13 at 9:28





                                1




                                1





                                you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

                                – Jeffrey Blattman
                                Jan 16 '14 at 18:54





                                you could write a class, JsonMap implements Map<String,Object to wrap JSONObject. then you get the true Map interface.

                                – Jeffrey Blattman
                                Jan 16 '14 at 18:54













                                Please add used libraries.

                                – mvermand
                                Jan 22 '18 at 6:56





                                Please add used libraries.

                                – mvermand
                                Jan 22 '18 at 6:56




                                1




                                1





                                @mvermand Added the dependency I've used back then

                                – Fabio Araujo
                                Jan 23 '18 at 11:02





                                @mvermand Added the dependency I've used back then

                                – Fabio Araujo
                                Jan 23 '18 at 11:02











                                4














                                I do it this way. It's Simple.



                                import java.util.Map;
                                import org.json.JSONObject;
                                import com.google.gson.Gson;

                                public class Main
                                public static void main(String[] args)
                                JSONObject jsonObj = new JSONObject(" "f1":"v1"");
                                @SuppressWarnings("unchecked")
                                Map<String, String> map = new Gson().fromJson(jsonObj.toString(),Map.class);
                                System.out.println(map);







                                share|improve this answer





























                                  4














                                  I do it this way. It's Simple.



                                  import java.util.Map;
                                  import org.json.JSONObject;
                                  import com.google.gson.Gson;

                                  public class Main
                                  public static void main(String[] args)
                                  JSONObject jsonObj = new JSONObject(" "f1":"v1"");
                                  @SuppressWarnings("unchecked")
                                  Map<String, String> map = new Gson().fromJson(jsonObj.toString(),Map.class);
                                  System.out.println(map);







                                  share|improve this answer



























                                    4












                                    4








                                    4







                                    I do it this way. It's Simple.



                                    import java.util.Map;
                                    import org.json.JSONObject;
                                    import com.google.gson.Gson;

                                    public class Main
                                    public static void main(String[] args)
                                    JSONObject jsonObj = new JSONObject(" "f1":"v1"");
                                    @SuppressWarnings("unchecked")
                                    Map<String, String> map = new Gson().fromJson(jsonObj.toString(),Map.class);
                                    System.out.println(map);







                                    share|improve this answer















                                    I do it this way. It's Simple.



                                    import java.util.Map;
                                    import org.json.JSONObject;
                                    import com.google.gson.Gson;

                                    public class Main
                                    public static void main(String[] args)
                                    JSONObject jsonObj = new JSONObject(" "f1":"v1"");
                                    @SuppressWarnings("unchecked")
                                    Map<String, String> map = new Gson().fromJson(jsonObj.toString(),Map.class);
                                    System.out.println(map);








                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Apr 5 '18 at 5:45

























                                    answered Aug 31 '17 at 12:57









                                    PavanPavan

                                    466




                                    466





















                                        3














                                        java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>().getType();
                                        Gson gson = new Gson();
                                        Map<String, Object> categoryicons = gson.fromJson(json, mapType );





                                        share|improve this answer



























                                          3














                                          java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>().getType();
                                          Gson gson = new Gson();
                                          Map<String, Object> categoryicons = gson.fromJson(json, mapType );





                                          share|improve this answer

























                                            3












                                            3








                                            3







                                            java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>().getType();
                                            Gson gson = new Gson();
                                            Map<String, Object> categoryicons = gson.fromJson(json, mapType );





                                            share|improve this answer













                                            java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>().getType();
                                            Gson gson = new Gson();
                                            Map<String, Object> categoryicons = gson.fromJson(json, mapType );






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Aug 13 '14 at 14:10









                                            newMaziarnewMaziar

                                            311




                                            311





















                                                3














                                                The JsonTools library is very complete. It can be found at Github.






                                                share|improve this answer

























                                                • @zygimantus, corrected the link, added new location.

                                                  – Bruno Ranschaert
                                                  Oct 6 '16 at 15:22















                                                3














                                                The JsonTools library is very complete. It can be found at Github.






                                                share|improve this answer

























                                                • @zygimantus, corrected the link, added new location.

                                                  – Bruno Ranschaert
                                                  Oct 6 '16 at 15:22













                                                3












                                                3








                                                3







                                                The JsonTools library is very complete. It can be found at Github.






                                                share|improve this answer















                                                The JsonTools library is very complete. It can be found at Github.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Oct 6 '16 at 15:22

























                                                answered Feb 14 '09 at 10:06









                                                Bruno RanschaertBruno Ranschaert

                                                4,12643142




                                                4,12643142












                                                • @zygimantus, corrected the link, added new location.

                                                  – Bruno Ranschaert
                                                  Oct 6 '16 at 15:22

















                                                • @zygimantus, corrected the link, added new location.

                                                  – Bruno Ranschaert
                                                  Oct 6 '16 at 15:22
















                                                @zygimantus, corrected the link, added new location.

                                                – Bruno Ranschaert
                                                Oct 6 '16 at 15:22





                                                @zygimantus, corrected the link, added new location.

                                                – Bruno Ranschaert
                                                Oct 6 '16 at 15:22











                                                2














                                                With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:



                                                Map map = gson.fromJson(json, Map.class);


                                                Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.






                                                share|improve this answer



























                                                  2














                                                  With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:



                                                  Map map = gson.fromJson(json, Map.class);


                                                  Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.






                                                  share|improve this answer

























                                                    2












                                                    2








                                                    2







                                                    With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:



                                                    Map map = gson.fromJson(json, Map.class);


                                                    Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.






                                                    share|improve this answer













                                                    With google's Gson 2.7 (probably earlier versions too, but I tested 2.7) it's as simple as:



                                                    Map map = gson.fromJson(json, Map.class);


                                                    Which returns a Map of type class com.google.gson.internal.LinkedTreeMap and works recursively on nested objects.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Sep 12 '16 at 5:51









                                                    isapirisapir

                                                    8,14864970




                                                    8,14864970





















                                                        1














                                                        One more alternative is json-simple which can be found in Maven Central:



                                                        (JSONObject)JSONValue.parse(someString); //JSONObject is actually a Map.


                                                        The artifact is 24kbytes, doesn't have other runtime dependencies.






                                                        share|improve this answer























                                                        • I get null back from a simple JSON string where one of the values is an array of strings.

                                                          – isapir
                                                          Sep 12 '16 at 5:45















                                                        1














                                                        One more alternative is json-simple which can be found in Maven Central:



                                                        (JSONObject)JSONValue.parse(someString); //JSONObject is actually a Map.


                                                        The artifact is 24kbytes, doesn't have other runtime dependencies.






                                                        share|improve this answer























                                                        • I get null back from a simple JSON string where one of the values is an array of strings.

                                                          – isapir
                                                          Sep 12 '16 at 5:45













                                                        1












                                                        1








                                                        1







                                                        One more alternative is json-simple which can be found in Maven Central:



                                                        (JSONObject)JSONValue.parse(someString); //JSONObject is actually a Map.


                                                        The artifact is 24kbytes, doesn't have other runtime dependencies.






                                                        share|improve this answer













                                                        One more alternative is json-simple which can be found in Maven Central:



                                                        (JSONObject)JSONValue.parse(someString); //JSONObject is actually a Map.


                                                        The artifact is 24kbytes, doesn't have other runtime dependencies.







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Mar 18 '16 at 10:58









                                                        pcjuzerpcjuzer

                                                        1,80432030




                                                        1,80432030












                                                        • I get null back from a simple JSON string where one of the values is an array of strings.

                                                          – isapir
                                                          Sep 12 '16 at 5:45

















                                                        • I get null back from a simple JSON string where one of the values is an array of strings.

                                                          – isapir
                                                          Sep 12 '16 at 5:45
















                                                        I get null back from a simple JSON string where one of the values is an array of strings.

                                                        – isapir
                                                        Sep 12 '16 at 5:45





                                                        I get null back from a simple JSON string where one of the values is an array of strings.

                                                        – isapir
                                                        Sep 12 '16 at 5:45











                                                        0














                                                        import net.sf.json.JSONObject

                                                        JSONObject.fromObject(yourJsonString).toMap





                                                        share|improve this answer



























                                                          0














                                                          import net.sf.json.JSONObject

                                                          JSONObject.fromObject(yourJsonString).toMap





                                                          share|improve this answer

























                                                            0












                                                            0








                                                            0







                                                            import net.sf.json.JSONObject

                                                            JSONObject.fromObject(yourJsonString).toMap





                                                            share|improve this answer













                                                            import net.sf.json.JSONObject

                                                            JSONObject.fromObject(yourJsonString).toMap






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Oct 26 '17 at 7:00









                                                            Li RaoLi Rao

                                                            1112




                                                            1112





















                                                                0














                                                                Underscore-java library can convert json string to hash map. I am the maintainer of the project.



                                                                Code example:



                                                                import com.github.underscore.lodash.U;
                                                                import java.util.*;

                                                                public class Main

                                                                @SuppressWarnings("unchecked")
                                                                public static void main(String[] args)
                                                                String json = ""
                                                                + " "data" :"
                                                                + " "
                                                                + " "field1" : "value1","
                                                                + " "field2" : "value2""
                                                                + " "
                                                                + "";

                                                                Map<String, Object> data = (Map) U.get((Map<String, Object>) U.fromJson(json), "data");
                                                                System.out.println(data);

                                                                // field1=value1, field2=value2







                                                                share|improve this answer





























                                                                  0














                                                                  Underscore-java library can convert json string to hash map. I am the maintainer of the project.



                                                                  Code example:



                                                                  import com.github.underscore.lodash.U;
                                                                  import java.util.*;

                                                                  public class Main

                                                                  @SuppressWarnings("unchecked")
                                                                  public static void main(String[] args)
                                                                  String json = ""
                                                                  + " "data" :"
                                                                  + " "
                                                                  + " "field1" : "value1","
                                                                  + " "field2" : "value2""
                                                                  + " "
                                                                  + "";

                                                                  Map<String, Object> data = (Map) U.get((Map<String, Object>) U.fromJson(json), "data");
                                                                  System.out.println(data);

                                                                  // field1=value1, field2=value2







                                                                  share|improve this answer



























                                                                    0












                                                                    0








                                                                    0







                                                                    Underscore-java library can convert json string to hash map. I am the maintainer of the project.



                                                                    Code example:



                                                                    import com.github.underscore.lodash.U;
                                                                    import java.util.*;

                                                                    public class Main

                                                                    @SuppressWarnings("unchecked")
                                                                    public static void main(String[] args)
                                                                    String json = ""
                                                                    + " "data" :"
                                                                    + " "
                                                                    + " "field1" : "value1","
                                                                    + " "field2" : "value2""
                                                                    + " "
                                                                    + "";

                                                                    Map<String, Object> data = (Map) U.get((Map<String, Object>) U.fromJson(json), "data");
                                                                    System.out.println(data);

                                                                    // field1=value1, field2=value2







                                                                    share|improve this answer















                                                                    Underscore-java library can convert json string to hash map. I am the maintainer of the project.



                                                                    Code example:



                                                                    import com.github.underscore.lodash.U;
                                                                    import java.util.*;

                                                                    public class Main

                                                                    @SuppressWarnings("unchecked")
                                                                    public static void main(String[] args)
                                                                    String json = ""
                                                                    + " "data" :"
                                                                    + " "
                                                                    + " "field1" : "value1","
                                                                    + " "field2" : "value2""
                                                                    + " "
                                                                    + "";

                                                                    Map<String, Object> data = (Map) U.get((Map<String, Object>) U.fromJson(json), "data");
                                                                    System.out.println(data);

                                                                    // field1=value1, field2=value2








                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Sep 28 '18 at 0:21

























                                                                    answered Jan 10 '16 at 15:01









                                                                    Valentyn KolesnikovValentyn Kolesnikov

                                                                    602813




                                                                    602813





















                                                                        -1














                                                                        JSON to Map always gonna be a string/object data type. i haved GSON lib from google.



                                                                        works very well and JDK 1.5 is the min requirement.






                                                                        share|improve this answer


















                                                                        • 1





                                                                          How did you use GSON?

                                                                          – NinjaCoder
                                                                          Jun 8 '15 at 20:08















                                                                        -1














                                                                        JSON to Map always gonna be a string/object data type. i haved GSON lib from google.



                                                                        works very well and JDK 1.5 is the min requirement.






                                                                        share|improve this answer


















                                                                        • 1





                                                                          How did you use GSON?

                                                                          – NinjaCoder
                                                                          Jun 8 '15 at 20:08













                                                                        -1












                                                                        -1








                                                                        -1







                                                                        JSON to Map always gonna be a string/object data type. i haved GSON lib from google.



                                                                        works very well and JDK 1.5 is the min requirement.






                                                                        share|improve this answer













                                                                        JSON to Map always gonna be a string/object data type. i haved GSON lib from google.



                                                                        works very well and JDK 1.5 is the min requirement.







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Jun 12 '09 at 16:03







                                                                        sadhasivam














                                                                        • 1





                                                                          How did you use GSON?

                                                                          – NinjaCoder
                                                                          Jun 8 '15 at 20:08












                                                                        • 1





                                                                          How did you use GSON?

                                                                          – NinjaCoder
                                                                          Jun 8 '15 at 20:08







                                                                        1




                                                                        1





                                                                        How did you use GSON?

                                                                        – NinjaCoder
                                                                        Jun 8 '15 at 20:08





                                                                        How did you use GSON?

                                                                        – NinjaCoder
                                                                        Jun 8 '15 at 20:08

















                                                                        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%2f443499%2fconvert-json-to-map%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

                                                                        SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                                                        은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현