首页 技术 正文
技术 2022年11月15日
0 收藏 300 点赞 4,472 浏览 5706 个字

json-simple是由是Google开发的Java JSON解析框架,基于Apache协议。目前版本为1.1

项目主页:https://code.google.com/p/json-simple/

Java实体类和JSON对象之间的映射如下表:

JSON Java
string java.lang.String
number java.lang.Number
true|false java.lang.Boolean
null null
array java.util.List
object java.util.Map

从此表中我们可以看出,当解析json对象映射到java实体类时,是从左边到右边。从左边到右边是Java实体类到json字符。在编码时默认的JSONArray是继承了ArrayList实现了List接口,JSONObject是继承了HashMap实现 了Map接口。jsonsimple默认的只支持表中的几种类型转换为json如果是一个复杂的对象要转换成Json字符,该类要实现JSONAware接口或者是JSONStreamAware。实现了以上两个接口后必须要重写toJSONString()或者writeJSONString()。来输出json字符。

好了,话不多说,看例子吧!

  • Example 1-1 – Encode a JSON object
       // 是java中HashMap的子类
JSONObject json = new JSONObject();
json.put("name", "张晓天");
json.put("boolean", true);
json.put("null", null);
json.put("num", 7);
json.put("double", 34.5);
printJson(json.toJSONString());
// {"num":7,"name":"张晓天","boolean":true,"double":34.5,"null":null}
  • Example 1-2 – Encode a JSON object – Streaming
JSONObject obj = new JSONObject();
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();
try {
obj.writeJSONString(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jsonText = out.toString();
printJson(jsonText);
// {"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}
  • Example 2-1 – Encode a JSON array
      JSONArray list = new JSONArray();
list.add("foot");
list.add("张晓天");
list.add(false);
list.add(6.9);
list.add(7);
list.add(null);
printJson(list.toJSONString());
// ["foot","张晓天",false,6.9,7,null]
  • Example 2-2 – Encode a JSON array – Using List
      List list = new LinkedList<>();
list.add("foot");
list.add("张晓天");
list.add(false);
list.add(6.9);
list.add(7);
list.add(null);
String jsonText = JSONValue.toJSONString(list);
printJson(jsonText);
// ["foot","张晓天",false,6.9,7,null]
  • Example 2-3- Encode a JSON array – Using List and streaming
       LinkedList list = new LinkedList();
list.add("张晓天");
list.add(100);
list.add(1000.21);
list.add(true);
list.add(null);
StringWriter out = new StringWriter();
try {
JSONValue.writeJSONString(list, out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jsonText = out.toString();
printJson(jsonText);
// ["张晓天",100,1000.21,true,null]
  • Example 3 – Merge two JSON objects
       JSONObject json1 = new JSONObject();
json1.put("name", "json1");
json1.put("age", 3);
json1.put("balance", 3.8); JSONObject json2 = new JSONObject();
json2.put("is_vip", "是");
json2.put("nickname", null);
json2.put("num", 8.9);
json2.putAll(json1); // 注意两个对象的key不能一样,否则会替换
printJson(json2.toJSONString());
// {"balance":3.8,"num":8.9,"nickname":null,"is_vip":"是","name":"json1","age":3}
  • Example 4 – Merge two JSON arrays
    JSONArray list1 = new JSONArray();
list1.add("foo");
list1.add(new Integer(100));
list1.add(new Double(1000.21)); JSONArray list2 = new JSONArray();
list2.add(new Boolean(true));
list2.add(null); JSONObject obj = new JSONObject();
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); obj.put("list1", list1);
obj.put("list2", list2);
printJson(obj.toJSONString()); // {"balance":1000.21,"list2":[true,null],"num":100,"list1":["foo",100,1000.21],"nickname":null,"is_vip":true,"name":"foo"}
  • Example 5-1 – Combination of JSON primitives, Map and List
        Map m1 = new LinkedHashMap();
Map m2 = new HashMap();
List l1 = new LinkedList(); m1.put("one", "第一值");
m1.put("two", "第二个值");
m2.put("k1", "m2-k1");
m2.put("k2", "m2-k2"); l1.add(m1);
l1.add(m2);
String json = JSONValue.toJSONString(l1);
printJson(json);
// [{"one":"第一值","two":"第二个值"},{"k1":"m2-k1","k2":"m2-k2"}]
  • Example 5-2 – Combination of JSON primitives, JSONObject, Map and List, and streaming
        StringWriter out = new StringWriter();
JSONObject obj = new JSONObject();
LinkedHashMap m1 = new LinkedHashMap();
LinkedList l1 = new LinkedList();
obj.put("k1", "v1");
obj.put("k2", m1);
obj.put("k3", l1);
m1.put("mk1", "mv1");
l1.add("lv1");
l1.add("lv2");
m1.put("mk2", l1);
try {
obj.writeJSONString(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("jsonString:");
System.out.println(out.toString());
String jsonString = obj.toJSONString();
System.out.println(jsonString);
  • Example 6 – Customize JSON outputs
JSONArray users = new JSONArray();
users.add(new User(123, "zxd", "zxd"));
users.add(new User(124, "ksks", "ksk"));
users.add(new User(125, "\"foo2\"", "secret2"));
printJson(users.toJSONString());
// [{userName:"zxd",ID:123},{userName:"ksks",ID:124},{userName:"\"foo2\"",ID:125}]
JSONArray users = new JSONArray();
users.add(new User(123, "foo1", "secret1"));
users.add(new User(124, "foo2", "secret2"));
users.add(new User(125, "\"foo2\"", "secret2"));
StringWriter out = new StringWriter();
try {
users.writeJSONString(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
printJson(out.toString()); //[{userName:"foo1",ID:123},{userName:"foo2",ID:124},{userName:"\"foo2\"",ID:125}]

class User implements JSONAware {
private int id;
private String name;
private String password; public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
} public String toJSONString() {
StringBuffer sb = new StringBuffer(); sb.append("{"); sb.append(JSONObject.escape("userName"));
sb.append(":");
sb.append("\"" + JSONObject.escape(name) + "\""); sb.append(","); sb.append(JSONObject.escape("ID"));
sb.append(":");
sb.append(id); sb.append("}");
// 也可以这样写
/*
* JSONObject obj = new JSONObject(); obj.put("userName", name);
* obj.put("ID", new Integer(id)); return obj.toString();
*/
return sb.toString(); } public void writeJSONString(Writer out) throws IOException {
LinkedHashMap obj = new LinkedHashMap();
obj.put("userName", name);
obj.put("ID", new Integer(id));
JSONValue.writeJSONString(obj, out);
}
}
 

如果你使用了maven来创建项目也可在pom中添加以下依赖:

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version></dependency>

总结:jsonsimple对于简单转换成json对象,但是对于 比较复杂的对象就不太好了,复杂的对象都要实现JSONAware重写对应的方法,才能实现指定格式的输出。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919