how should i convert a map to json String with gson
See original GitHub issueMy 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:
- Created 6 years ago
- Reactions:2
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes. This is working as intended.
You are trying to serialize an anonymous inner class. Don’t.