【JAVA】JSONObject的用法
配置
<!--json--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- JSONObject对象依赖的jar包 --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> <!--jdk15并不是jdk版本,而是net.sf.json-lib的一个版本,对应的jdk版本是jdk1.5以上的版本--> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!--将Java对象与json对象和xml互转--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
1、通过原生生成json
package cn.demo; import org.junit.*; import java.util.*; import com.alibaba.fastjson.*; public class AloneTest { @Test public void alone(){ // json串 JSONObject data = new JSONObject(); data.put("name", "zhansan"); data.put("age", "18"); data.put("address", "beijing xicheng"); System.out.println(data); // 列表 JSONArray content = new JSONArray(); content.add("a"); content.add("c"); content.add("nice"); System.out.println(content); return; } }
执行结果:
2、通过hashMap数据结构生成
package cn.demo; import org.junit.*; import java.util.*; import com.alibaba.fastjson.*; public class AloneTest { @Test public void alone(){ // ……
//通过hashMap数据结构生成 Map<String, Object> map = new HashMap<>(); map.put("name", "wangwu"); map.put("age", "20"); map.put("address", "beijing shijingshan"); System.out.println(new JSONObject(map).toJSONString()); return; } }
执行结果:
3、通过实体生成
package cn.demo; import org.junit.*; import java.util.*; import com.alibaba.fastjson.*; public class AloneTest { public class Student{ private String name; private int age; private String address; public void setName(String name){ this.name=name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } } @Test public void alone(){ // 通过实体生成 Student stu = new Student(); stu.setName("wanwu"); stu.setAge(22); stu.setAddress("shanghai"); JSONObject stuJson = (JSONObject) JSON.toJSON(stu); System.out.println(stuJson); return; } }
执行结果:
4、JSON字符串转换成JSON对象
package cn.demo; import org.junit.*; import java.util.*; import com.alibaba.fastjson.*; public class AloneTest { @Test public void alone(){ // JSON字符串转换成JSON对象 String strJson = "{\"address\":\"dishini\",\"name\":\"wanlu\",\"age\":33}"; JSONObject jo = JSONObject.parseObject(strJson); System.out.println(jo); return; } }
执行结果:
5、list对象与listJson的转换
package cn.demo; import org.junit.*; import java.util.*; import com.alibaba.fastjson.*; public class AloneTest { public class Student{ private String name; private int age; private String address; public void setName(String name){ this.name=name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } } @Test public void alone(){ //list对象转listJson ArrayList<Student> studentList = new ArrayList(); Student stu = new Student(); stu.setName("chongwen"); stu.setAge(32); stu.setAddress("acheng"); studentList.add(stu); Student st = new Student(); st.setName("anjing"); st.setAge(18); st.setAddress("wuchang"); studentList.add(st); //list转json字符串 String str = JSON.toJSON(studentList).toString(); System.out.println(str); //json字符串转listJson格式 JSONArray ja = JSONObject.parseArray(str); System.out.println(ja); return; } }
执行结果:
-------------------------------------------------------------------------------------
如果万事开头难 那请结局一定圆满 @ Phoenixy
-------------------------------------------------------------------------------------