Java基础之Json
2015-12-26 00:00
621 查看
摘要: Json越使用到的地方越来越多,本身使用的方式也很简单。这里介绍下json的基本概念,已经阿里的fastjson使用。
1.1简述
Json由于结构简单(key:value),生成和解析的速度很快,在网络中越来越多的使用于传送、存储(mongodb,localStorage等)。
b) 包涵数字:{"key":[ {"key":" value", "key":" value"},{"key":" value", "key":" value"}]}
c) 字符串: '{"key":" value", "key":" value"}'
Java基础之json
一.概述
1.1简述
Json由于结构简单(key:value),生成和解析的速度很快,在网络中越来越多的使用于传送、存储(mongodb,localStorage等)。2.2 json构成
a) 对象:{"key":" value", "key":" value"}b) 包涵数字:{"key":[ {"key":" value", "key":" value"},{"key":" value", "key":" value"}]}
c) 字符串: '{"key":" value", "key":" value"}'
二.案例
package json; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.alibaba.fastjson.JSON; public class JsonTest { @Test public void TestJson() { //基本对象 Jsons js1 = new Jsons(); js1.setJsonId(12); js1.setJsonName("你好"); System.out.println(JSON.toJSON(js1));// {"jsonId":12,"jsonName":"你好"} System.out.println(JSON.toJSONString(js1));// {"jsonId":12,"jsonName":"你好"} //包涵对象 Jsons2 js2 = new Jsons2(); js2.setJsonId(12); js2.setJsonName("子"); JsonParent jp = new JsonParent(); jp.setJsonParentId(22); jp.setJsonParentName("父"); js2.setJsonParent(jp); System.out.println(JSON.toJSON(js2));// {"jsonParent":{"jsonParentId":22,"jsonParentName":"父"},"jsonId":12,"jsonName":"子"} System.out.println(JSON.toJSONString(js2));// {"jsonId":12,"jsonName":"子","jsonParent":{"jsonParentId":22,"jsonParentName":"父"}} //包涵对象数组 Jsons js3 = new Jsons(); js3.setJsonId(12); js3.setJsonName("子"); JsonParent jp1 = new JsonParent(); jp1.setJsonParentId(22); jp1.setJsonParentName("父"); List<JsonParent> list = new ArrayList<JsonParent>(); list.add(jp1); js3.setJsonParents(list); System.out.println(JSON.toJSON(js3));// {"jsonId":12,"jsonName":"子","jsonParents":[{"jsonParentId":22,"jsonParentName":"父"}]} System.out.println(JSON.toJSONString(js3));// {"jsonId":12,"jsonName":"子","jsonParents":[{"jsonParentId":22,"jsonParentName":"父"}]} //空对象数字 Jsons js4 = new Jsons(); js4.setJsonId(12); js4.setJsonName("子"); JsonParent jp2 = new JsonParent(); List<JsonParent> list1 = new ArrayList<JsonParent>(); list.add(jp2); js4.setJsonParents(list1); System.out.println(JSON.toJSON(js4));//{"jsonId":12,"jsonName":"子","jsonParents":[]} System.out.println(JSON.toJSONString(js4));//{"jsonId":12,"jsonName":"子","jsonParents":[]} } } class Jsons2 implements Serializable { private static final long serialVersionUID = 1L; private Integer jsonId; private String jsonName; private JsonParent jsonParent; public JsonParent getJsonParent() { return jsonParent; } public void setJsonParent(JsonParent jsonParent) { this.jsonParent = jsonParent; } public Integer getJsonId() { return jsonId; } public void setJsonId(Integer jsonId) { this.jsonId = jsonId; } public String getJsonName() { return jsonName; } public void setJsonName(String jsonName) { this.jsonName = jsonName; } } class Jsons implements Serializable { private static final long serialVersionUID = 1L; private Integer jsonId; private String jsonName; private List<JsonParent> jsonParents; public Integer getJsonId() { return jsonId; } public void setJsonId(Integer jsonId) { this.jsonId = jsonId; } public String getJsonName() { return jsonName; } public void setJsonName(String jsonName) { this.jsonName = jsonName; } public List<JsonParent> getJsonParents() { return jsonParents; } public void setJsonParents(List<JsonParent> jsonParents) { this.jsonParents = jsonParents; } } class JsonParent implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer jsonParentId; private String jsonParentName; public Integer getJsonParentId() { return jsonParentId; } public void setJsonParentId(Integer jsonParentId) { this.jsonParentId = jsonParentId; } public String getJsonParentName() { return jsonParentName; } public void setJsonParentName(String jsonParentName) { this.jsonParentName = jsonParentName; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树