Java对JSON的简单操作
来源:互联网 发布:python 分割文件 编辑:程序博客网 时间:2024/06/05 16:55
JSONArray和JSONObject
基本用法
package com.cloud.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
publicclass Demo1 {
//创建JSONObject对象
publicstatic JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("username","Spring");
jsonObject.put("sex","男");
jsonObject.put("QQ","123456");
return jsonObject;
}
@SuppressWarnings("static-access")
publicstatic void main(String[] args)throws Exception {
//创建一个JSONObject对象
JSONObject jsonObject = Demo1.createJSONObject();
//输出jsonObject:{"sex":"男","username":"Spring","QQ":"123456"}
System.out.println(jsonObject);
//判断jsonObject的对象类
System.out.println("数组?"+jsonObject.isArray()+";为空?"+
jsonObject.isEmpty()+";isNullObject?"+
jsonObject.isNullObject()+";");
//追加元素属性
jsonObject.put("address","安徽合肥");
System.out.println(jsonObject);
//返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "Value0");
jsonArray.add(1, "Value1");
jsonObject.element("jsonArray", jsonArray);
//在jsonObject后面住家一个jsonArray
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println(jsonObject);
System.out.println("返回一个JSONArray对象:" + array);
//根据key返回字符串
String username = jsonObject.getString("username");
System.out.println("username:"+username);
//字符串转换为jsonObject
String temp = jsonObject.toString();
System.out.println(temp);
JSONObject object = jsonObject.fromObject(temp);
System.out.println("QQ:"+object.get("QQ"));
}
}
案例
package com.cloud.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
publicclass Demo2 {
publicstatic void main(String[] args) {
JSONObject jsonObject0 = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
JSONObject jsonObject3 = new JSONObject();
JSONArray jsonArray = new JSONArray();
//jsonObject0
jsonObject0.put("name0","Spring");
jsonObject0.put("sex0","man");
System.out.println("jsonObject0:"+jsonObject0);
//jsonObject1
jsonObject1.put("name1","summer");
jsonObject1.put("sex1","woman");
System.out.println("jsonObject1"+jsonObject1);
//jsonObject2
jsonObject2.put("item0", jsonObject0);
jsonObject2.put("item1", jsonObject1);
System.out.println("jsonObject2"+jsonObject2);
//jsonObject3
jsonObject3.put("jo31", jsonObject2);
jsonObject3.put("jo32", jsonObject3);
System.out.println("jsonObject3"+jsonObject3);
//往JSONArray中添加JSONObject对象。发现JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[]
jsonArray.add(jsonObject1);
System.out.println(jsonArray);
JSONObject jsonObject4 = new JSONObject();
jsonObject4.element("weater", jsonArray);
System.out.println("jsonObject4:"+jsonObject4);
}
}
FromObject和toBean
fromObject
package com.cloud.test;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
publicclass fo {
@SuppressWarnings("rawtypes")
publicstatic void main(String[] args) {
Map map = new HashMap();
map.put("spring","spring1");
map.put("summer","summer1");
map.put("autumn","autumn1");
JSONObject json = JSONObject.fromObject(map);
//{"autumn":"autumn1","summer":"summer1","spring":"spring1"}
System.out.println(json);
}
}
toBean
student.java实体类
package com.cloud.test;
publicclass Student{
privateint id;
private Stringname;
privateint age;
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age = age;
}
public String toString(){
returnthis.id+";"+this.name+";"+this.age;
}
}
tb.java
package com.cloud.test;
import net.sf.json.JSONObject;
publicclass tb {
publicstatic void main(String[] args) {
String json = "{id:'101',name:'你好',age:'22'}";
Student stu = new Student();
JSONObject jsonObject = JSONObject.fromObject(json);
stu = (Student) JSONObject.toBean(jsonObject, Student.class);
//101;你好;22
System.out.println(stu);
String json1 = "{id:'101',name:'张三'}";
Student stu1 = new Student();
JSONObject obj = JSONObject.fromObject(json1);
System.out.println("obj:"+obj);
stu1 = (Student)JSONObject.toBean(obj, Student.class);
//101;张三;0
System.out.println(stu1);
String json2 = "{id:'101',age:'22'}";
Student stu2 = new Student();
JSONObject obj1 = JSONObject.fromObject(json2);
stu2 = (Student)JSONObject.toBean(obj1, Student.class);
//101;null;22
System.out.println(stu2);
String json3 = "{id:'101',name:'张三',age:'nn'}";
Student stu3 = new Student();
JSONObject obj2 = JSONObject.fromObject(json3);
stu3 = (Student)JSONObject.toBean(obj2, Student.class);
//101;张三;0
System.out.println(stu3);
String json4 = "{id:'101',name:'张三',age:'22',sex:'男'}";
Student stu4 = new Student();
JSONObject obj3 = JSONObject.fromObject(json4);
stu4 = (Student)JSONObject.toBean(obj3, Student.class);
//101;张三;22
System.out.println(stu4);
}
}
- Java对JSON的简单操作
- java对json的操作
- js对json的简单操作
- java 对json操作的备忘
- jquery 对 cookie存储 json格式的简单操作
- Spark对HDFS上json数据的简单操作
- Java 中对剪贴板的简单操作
- java对文件拷贝的简单操作
- Java 对JavaBean的简单内省操作
- java对execl的简单操作
- java对rendis 的简单操作
- Java对Mysql的简单数据操作
- Java对cookie的简单操作
- Json的简单操作
- 对json的简单理解
- python对json的操作
- javascript对json的操作
- js对json 的操作
- Atom 安装插件失败的解决方案
- Git学习 <day6>-Fast-forward
- Ubuntu下让终端走SS代理的方法
- Java中的Arrays.asList()方法
- 通过XmlDocument读写Xml文档
- Java对JSON的简单操作
- jcaptcha+使用redis集群保存jcaptcha
- Swift-字符和字符串
- 《Linux内核分析》 之 计算机是如何工作的。1
- unix环境高级编程-读书笔记(7)
- scrollTop的兼容问题
- reactjs 里map的使用
- 利用Fragment实现列表内容显示
- BZOJ1497: [NOI2006]最大获利