转换Map< String,String>到json [英] Converting Map<String,String> into json
本文介绍了转换Map< String,String>到json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Java中有这样的Map<String,String>
:
I have Map<String,String>
in java like this :
{card_switch=Master, issuing_bank=ICCI, card_Type=DebitCard}
我正在使用简单json解析器将此地图解析为json对象.
I'm using the simple json parser to parse this map into json object.
我尝试过:
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)
是否可以将Map<String,String>
转换为json?
Is that possible to convert Map<String,String>
into json?
推荐答案
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);
这篇关于转换Map< String,String>到json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文