Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Map<String,String> into json

Tags:

java

json

I have Map<String,String> in java like this :

{card_switch=Master, issuing_bank=ICCI, card_Type=DebitCard}

I'm using the simple json parser to parse this map into json object.

I tried :

Object json = JSONValue.parse(entry.getKey());

But I get an error message :

Object json = JSONValue.parse(entry.getKey());
                      ^
method JSONValue.parse(String) is not applicable
  (actual argument Map<String,String> cannot be converted to String by method invocation conversion)
method JSONValue.parse(Reader) is not applicable
  (actual argument Map<String,String> cannot be converted to Reader by method invocation conversion)

Is that possible to convert Map<String,String> into json?

like image 653
sriram Avatar asked Oct 10 '12 10:10

sriram


People also ask

How do I convert a string to JSON?

String data can be easily converted to JSON using the stringify() function, and also it can be done using eval() , which accepts the JavaScript expression that you will learn about in this guide.

Can I store a Map in JSON?

We can convert a Map to JSON object using the toJSONString() method(static) of org. json. simple. JSONValue.

Can I convert a string to Map in Java?

We can also convert an array of String to a HashMap. Suppose we have a string array of the student name and an array of roll numbers, and we want to convert it to HashMap so the roll number becomes the key of the HashMap and the name becomes the value of the HashMap. Note: Both the arrays should be the same size.


3 Answers

You can also try something like this with Gson Library:

package com.stackoverflow.works;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/*
 * @Author: sarath_sivan
 */

public class MapToJsonConverter {

    /*
     * @Description: Method to convert Map to JSON String
     * @param: map Map<String, String> 
     * @return: json String
     */
    public static String convert(Map<String, String> map) {
        Gson gson = new Gson();
        String json = gson.toJson(map);
        return json;
    }

    /*
     * @Description: Method to convert JSON String to Map
     * @param: json String 
     * @return: map Map<String, String> 
     */
    public static Map<String, String> revert(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, String>>(){}.getType();
        Map<String, String> map = gson.fromJson(json, type);
        return map;
    }

    /*
     * @Description: Method to print elements in the Map
     * @param: map Map<String, String> 
     * @return: void 
     */
    public static void printMap(Map<String, String> map) {
        for (String key : map.keySet()) {
            System.out.println("map.get(\"" + key + "\") = " + map.get(key));
        }
    }

    /*
     * @Description: Method to print the JSON String
     * @param: json String 
     * @return: void 
     */
    public static void printJson(String json) {
        System.out.println("json = " + json);
    }

    /*
     * @Description: Main method to test the JSON-MAP convert/revert logic
     */
    public static void main(String[] args) {
        Map<String, String> paymentCards = new HashMap<String, String>();
        paymentCards.put("card_switch", "Master");
        paymentCards.put("issuing_bank", "ICCI");
        paymentCards.put("card_Type", "DebitCard");

        String json = convert(paymentCards); //converting Map to JSON String
        System.out.println("Map to JSON String");
        System.out.println("******************");
        printJson(json); 

        System.out.println();

        paymentCards = revert(json); //converting JSON String to Map
        System.out.println("JSON String to Map");
        System.out.println("******************");
        printMap(paymentCards);
    }

}

The output look like this:

Output

like image 102
1218985 Avatar answered Oct 17 '22 04:10

1218985


Have a look at example 1.4 on this page http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-4_-_Encode_a_JSON_object_-_Using_Map_and_streaming:

 Map obj=new LinkedHashMap();
   obj.put("name","foo");
   obj.put("num",new Integer(100));
   obj.put("balance",new Double(1000.21));
   obj.put("is_vip",new Boolean(true));
   obj.put("nickname",null);
   StringWriter out = new StringWriter();
   JSONValue.writeJSONString(obj, out);
   String jsonText = out.toString();
   System.out.print(jsonText);
like image 35
Paul Tomblin Avatar answered Oct 17 '22 03:10

Paul Tomblin


Try this. But do you need the gson library:

Map<String, Object> map = new HashMap<>();
String value = new Gson().toJson(map);
like image 3
Edy Huiza Avatar answered Oct 17 '22 05:10

Edy Huiza