[英]Converting Map<String,String> into json
I have Map<String,String>
in java like this : 我在java中有
Map<String,String>
,如下所示:
{card_switch=Master, issuing_bank=ICCI, card_Type=DebitCard}
I'm using the simple json parser to parse this map into json object. 我正在使用简单的json解析器将此映射解析为json对象。
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? 可以将
Map<String,String>
转换为json吗?
You can also try something like this with Gson Library: 您也可以使用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: 输出看起来像这样:
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 : 请查看此页面上的示例1.4 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);
Try this. 试试这个。 But do you need the gson library:
但是你需要gson库吗:
Map<String, Object> map = new HashMap<>();
String value = new Gson().toJson(map);
It should be possible, but i think you hold it the wrong way around: parse will PARSE json-text content, and provide you with an Java-equivalent ("decoding") 它应该是可能的,但我认为你持有它错误的方法:解析PARSE json-text内容,并为你提供Java等价物(“解码”)
look at the sample on the homepage: http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-3_-_Encode_a_JSON_object_-_Using_Map 请查看主页上的示例: http : //code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-3_-_Encode_a_JSON_object_-_Using_Map
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.