I am leaning Java.
I have to transfer a Hashmap to Server using rpc.
HashMap
Map<String, String> testMap = new HashMap<String, String>();
testMap .put("1", "abc");
testMap .put("2", "ezc");
testMap .put("3", "afc");
testMap .put("4", "cvc");
..
how to do that.
We can convert a Map to JSON object using the toJSONString() method(static) of org. json. simple. JSONValue.
JSON is a text based object that different from HashMap.
You can map the data types of your business model into JSON by using the examples. Data in JSON is either an object or an array. A JSON object is an unordered collection of names and values. A JSON array is an ordered sequence of values. A value can be a string, a number, a Boolean, a null, an object, or an array.
We can easily convert JSON data into a map because the JSON format is essentially a key-value pair grouping and the map also stores data in key-value pairs. Let's understand how we can use both JACKSON and Gson libraries to convert JSON data into a Map.
Take a look at Jackson JSON processor. In particular the code will look something like:
Map map = your map
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);
If you want pretty JSON (multiple lines) for debugging, then use:
String json = mapper.defaultPrettyPrintingWriter().writeValueAsString(map);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With