java的json操作_java中常见的JSON操作

一、什么是Json

Json是指JavaScript对象表示法(Java Script Object Notation)

Json是轻量级的文本数据交换格式

Json独立于语言和平台:Json解析器和Json库支持许多不同的编程语言

Json具有自我描述性,更易理解

二、Java中操作Json的几种方式

FastJson 阿里巴巴开发的 JSON 库,性能十分优秀

com.alibaba

fastjson

1.2.62

Gson 谷歌开发的 JSON 库,功能十分全面

com.google.code.gson

gson

2.8.6

net.sf.json 最常见,使用较多

net.sf.json-lib

json-lib

2.4

jdk15

Jackson 社区十分活跃且更新速度很快。

com.fasterxml.jackson.core

jackson-core

2.10.0

1、FastJson示例

package com.powersi.test;

import java.util.ArrayList;

import java.util.List;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;

public class FastJsonTest {

/**

* java对象转Json

*/

public static String object2Json(){

JSONObject json = new JSONObject();

Student stu = new Student();

stu.setId(001);

stu.setName("张三");

stu.setAge(18);

json = (JSONObject) JSONObject.toJSON(stu);

return json.toString();

}

/**

* json转java对象

*/

public static String json2Object(){

JSONObject json = new JSONObject();

json.put("id", "001");

json.put("name", "张三");

json.put("age", "18");

Student s = JSONObject.toJavaObject(json, Student.class);

return s.toString();

}

/**

* json转字符串

*/

public static String json2String(){

JSONObject json = new JSONObject();

json.put("id", "001");

json.put("name", "张三");

json.put("age", "18");

String s = JSONObject.toJSONString(json);

return s;

}

/**

* 字符串转json

*/

public static String string2Json(){

JSONObject json = new JSONObject();

String s = "{\"id\":\"001\", \"name\":\"张三\", \"age\":\"18\"}";

json = JSONObject.parseObject(s);

return json.toString();

}

/**

* 集合转json数组

*/

public static String list2JsonArray(){

Student stu = new Student();

stu.setId(001);

stu.setName("张三");

stu.setAge(18);

Student stu2 = new Student();

stu2.setId(002);

stu2.setName("李四");

stu2.setAge(19);

List stuList = new ArrayList();

stuList.add(stu);

stuList.add(stu2);

String s = JSONArray.toJSONString(stuList);

return s;

}

public static void main(String[] args) {

System.out.println(FastJsonTest.list2JsonArray());

}

}

2、Gson示例

package com.powersi.test;

import java.util.ArrayList;

import java.util.List;

import com.google.gson.Gson;

public class GsonTest {

/**

* java对象转Json

*/

public static String object2Json(){

Gson gson = new Gson();

Student stu = new Student();

stu.setId(001);

stu.setName("张三");

stu.setAge(18);

String s = gson.toJson(stu);

return s;

}

/**

* json转java对象

*/

public static String json2Object(){

Gson gson = new Gson();

String jsonStr = object2Json();

Student s = gson.fromJson(jsonStr, Student.class);

return s.toString();

}

/**

* 对象转json数组

*/

public static String list2JsonArray(){

Gson gson = new Gson();

Student stu = new Student();

stu.setId(001);

stu.setName("张三");

stu.setAge(18);

Student stu2 = new Student();

stu2.setId(002);

stu2.setName("李四");

stu2.setAge(19);

List stuList = new ArrayList();

stuList.add(stu);

stuList.add(stu2);

String s = gson.toJson(stuList);

return s;

}

public static void main(String[] args) {

System.out.println(GsonTest.list2JsonArray());

}

}

3、net.sf.json示例

package com.powersi.test;

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class NetSFJsonTest {

/**

* java对象转Json

*/

public static String object2Json(){

JSONObject json = new JSONObject();

Student stu = new Student();

stu.setId(001);

stu.setName("张三");

stu.setAge(18);

json = JSONObject.fromObject(stu);

return json.toString();

}

/**

* json转java对象

*/

public static String json2Object(){

String jsonStr = object2Json();

Student stu = (Student)JSONObject.toBean(JSONObject.fromObject(jsonStr), Student.class);

return stu.toString();

}

/**

* 对象转json数组

*/

public static String list2JsonArray(){

Student stu = new Student();

stu.setId(001);

stu.setName("张三");

stu.setAge(18);

Student stu2 = new Student();

stu2.setId(002);

stu2.setName("李四");

stu2.setAge(19);

List stuList = new ArrayList();

stuList.add(stu);

stuList.add(stu2);

JSONArray jsonArray = JSONArray.fromObject(stuList);

return jsonArray.toString();

}

public static void main(String[] args) {

System.out.println(NetSFJsonTest.list2JsonArray());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值