将java Console输出的JSON格式字符串转化得易读的JSON代码网址
JSONObject与java中的String,map,Object等的转换
【1】org.json包的常用方法
<1>创建JSONObject对象并直接添加内容
public class JSONStudy {
public static void main(String[] args) {
TestJSONObject();
}
public static void TestJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "食人魔魔法师");
jsonObject.put("type", "弱智型");
jsonObject.put("技能", new String[]{"爆裂轰炸","多重施法"});
jsonObject.put("是否超神?", false);
System.out.println(jsonObject.toString()); //将JSON转化为字符串输出
}
}
输出结果:
{
"name": "食人魔魔法师",
"是否超神?": false,
"type": "弱智型",
"技能": [
"爆裂轰炸",
"多重施法"
]
}
<2>将map转化为json对象并输出
public static void TestMapToJSON(){
Map<String,Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("name", "祈求者");
jsonMap.put("type", "弱智型");
jsonMap.put("技能", new String[]{"天火","磁暴"});
jsonMap.put("是否超神?", true);
System.out.println(new JSONObject(jsonMap).toString()); //将map转化为JSONObject输出
}
结果为
{
"name": "祈求者",
"是否超神?": true,
"type": "弱智型",
"技能": [
"天火",
"磁暴"
]
}
<3>将POJO类转化为JSONObject
public static void TestPOJOToJSON(){
Hero jsonHero = new Hero();
jsonHero.setName("幻影刺客");
jsonHero.setType("敏捷型");
jsonHero.setTalent(new String[]{"窒息之刃","恩赐解脱"});
jsonHero.setHolyShit(true);
System.out.println(new JSONObject(jsonHero).toString());
}
输出结果为
{
"holyShit": true,
"name": "幻影刺客",
"talent": [
"窒息之刃",
"恩赐解脱"
],
"type": "敏捷型"
}
【2】fastjson包
<1>// 将Java对象序列化为JSON字符串,支持各种各种Java基本类型和JavaBean
// public static String toJSONString(Object object, SerializerFeature… features);
例子:
String jsonString = JSON.toJSONString(jsonHero,true);
System.out.println(jsonString);
输出
{
"holyShit":true,
"name":"幻影刺客",
"talent":["窒息之刃","恩赐解脱"],
"type":"敏捷型"
}
<2>// 将Java对象序列化为JSON字符串,返回JSON字符串的utf-8 bytes
// public static byte[] toJSONBytes(Object object, SerializerFeature… features);
byte[] jsonbyte = JSON.toJSONBytes(jsonHero);
System.out.println(Arrays.toString(jsonbyte));
[123, 34, 104, 111, 108, 121, 83, 104, 105, 116, 34, 58, 116, 114, 117, 101, 44, 34, 110,
97, 109, 101, 34, 58, 34, -27, -71, -69, -27, -67, -79, -27, -120, -70, -27, -82, -94, 34, 44, 34,
116, 97, 108, 101, 110, 116, 34, 58, 91, 34, -25, -86, -110, -26, -127, -81, -28, -71, -117, -27,
-120, -125, 34, 44, 34, -26, -127, -87, -24, -75, -112, -24, -89, -93, -24, -124, -79, 34, 93, 44, 3
4,116, 121, 112, 101, 34, 58, 34, -26, -107, -113, -26, -115, -73, -27, -98, -117, 34, 125]
<3>// public static final Object toJSON(Object javaObject);
// 将JavaBean转换为JSONObject或者JSONArray。
com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) JSON.toJSON(jsonHero);
System.out.println(jsonObject.toString());
{"holyShit":true,"name":"幻影刺客","talent":["窒息之刃","恩赐解脱"],"type":"敏捷型"}
<4>json文本转化为其他
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
String jsonString = JSON.toJSONString(jsonHero,true);
com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) JSON.parse(jsonString);
System.out.println(jsonObject.toString());
Hero hero = JSON.parseObject(jsonString, Hero.class);
System.out.println(hero.toString());
结果为:
{"holyShit":true,"name":"幻影刺客","talent":["窒息之刃","恩赐解脱"],"type":"敏捷型"}
Hero [name=幻影刺客, type=敏捷型, talent=[窒息之刃, 恩赐解脱], holyShit=true]
<5>map与JSONObject互转
例子:
public static void TestMapToJSON(){
Map<String,Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("name", "祈求者");
jsonMap.put("type", "弱智型");
jsonMap.put("技能", new String[]{"天火","磁暴"});
jsonMap.put("是否超神?", true);
String jsonString = JSON.toJSONString(jsonMap,true);
//Map转化为JSON string
System.out.println(jsonString);
Map<String, Object> jsonMap2 = new HashMap<String, Object>();
//JSON string 转化为 Map
jsonMap2 = (Map<String, Object>) JSON.parse(jsonString);
for (String key : jsonMap2.keySet()) {
System.out.println(key+":"+jsonMap2.get(key));
}
}
输出结果:
{
"name":"祈求者",
"是否超神?":true,
"type":"弱智型",
"技能":["天火","磁暴"]
}
name:祈求者
是否超神?:true
type:弱智型
技能:["天火","磁暴"]