question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how should i convert a map to json String with gson

See original GitHub issue

My code likes:


    @Test
    public void testGsonTransferMap(){
        HashMap<String, Object> map = new HashMap<String, Object>() {{
            put("Request", new HashMap<String ,Object>(){{
                put("Data",new HashMap<String,Object>(){{
                    put("NWExID", "7019");
                    put("OrgOrderNo", "123");
                    put("OrgTransDate", "20170518");
                }});
            }});
        }};
        Gson gson = new Gson();
        Type gsonType = new TypeToken<HashMap<String ,Object>>(){}.getType();
        String gsonString = gson.toJson(map,gsonType);
        System.out.println(gsonString);
    } 

and the output is : “{}” so,what’s wrong with my code , how should i do.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
JakeWhartoncommented, Jun 22, 2017

Yes. This is working as intended.

14reactions
NightlyNexuscommented, May 23, 2017

You are trying to serialize an anonymous inner class. Don’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert map to json string using Gson APIs?
How to convert map to json string using Gson APIs? ; package com.java2novice.json.gson; ; import java.util.HashMap;. import java.util.Map; ; import com.google.gson ...
Read more >
Convert Hashmap to JSON using GSON - java - Stack Overflow
Basically, there's no way to get Gson to automatically convert your strings to numeric values. If you want them to show up as...
Read more >
Convert a Map to JSON using the Gson library in Java?
A Gson is a library that can be used to parse Java objects to JSON and vice-versa. It can also be used to...
Read more >
How to Convert Java Map to JSON - DevQA.io
In this post, we look at 3 different examples to convert Java Map to JSON. We will be using Jackson, Gson and org.json...
Read more >
Convert JSON to a Map Using Gson - Baeldung
Now, if we construct our Map type as Map<String, Object>, then the parser will still default as we saw in the previous section....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found