JSONObjec的作用详解——Web网络系列学习笔记
Posted 来老铁干了这碗代码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSONObjec的作用详解——Web网络系列学习笔记相关的知识,希望对你有一定的参考价值。
JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。
总而言之,JSONObject是一种中间结构,并且在网络中有很快的传输速度。
1. 依赖包:fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
2. 通过原生生成json数据格式。
JSONObject zhangsan = new JSONObject();
try {
//添加
zhangsan.put("name", "张三");
zhangsan.put("age", 18.4);
zhangsan.put("birthday", "1900-20-03");
zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
zhangsan.put("null", null);
zhangsan.put("house", false);
System.out.println(zhangsan.toString());
} catch (JSONException e) {
e.printStackTrace();
}
3. 通过hashMap数据结构生成
HashMap<String, Object> zhangsan = new HashMap<>();
zhangsan.put("name", "张三");
zhangsan.put("age", 18.4);
zhangsan.put("birthday", "1900-20-03");
zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
zhangsan.put("null", null);
zhangsan.put("house", false);
System.out.println(new JSONObject(zhangsan).toString());
4. 通过实体生成
Student student = new Student();
student.setId(1);
student.setAge("20");
student.setName("张三");
//生成json格式
System.out.println(JSON.toJSON(student));
//对象转成string
String stuString = JSONObject.toJSONString(student);
5. JSON字符串转换成JSON对象
String studentString = "{\\"id\\":1,\\"age\\":2,\\"name\\":\\"zhang\\"}";
//JSON字符串转换成JSON对象
JSONObject jsonObject1 = JSONObject.parseObject(stuString);
System.out.println(jsonObject1);
6. list对象转listJson
ArrayList<Student> studentLsit = new ArrayList<>();
Student student1 = new Student();
student1.setId(1);
student1.setAge("20");
student1.setName("asdasdasd");
studentLsit.add(student1);
Student student2 = new Student();
student2.setId(2);
student2.setAge("20");
student2.setName("aaaa:;aaa");
studentLsit.add(student2);
//list转json字符串
String string = JSON.toJSON(studentLsit).toString();
System.out.println(string);
//json字符串转listJson格式
JSONArray jsonArray = JSONObject.parseArray(string);
System.out.println(jsonArray);
7. fastjson包中的Json源码
// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package com.alibaba.fastjson;
public class JSONObject extends com.alibaba.fastjson.JSON implements java.util.Map<java.lang.String,java.lang.Object>, java.lang.Cloneable, java.io.Serializable, java.lang.reflect.InvocationHandler {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private final java.util.Map<java.lang.String,java.lang.Object> map;
public JSONObject() { /* compiled code */ }
public JSONObject(java.util.Map<java.lang.String,java.lang.Object> map) { /* compiled code */ }
public JSONObject(boolean ordered) { /* compiled code */ }
public JSONObject(int initialCapacity) { /* compiled code */ }
public JSONObject(int initialCapacity, boolean ordered) { /* compiled code */ }
public int size() { /* compiled code */ }
public boolean isEmpty() { /* compiled code */ }
public boolean containsKey(java.lang.Object key) { /* compiled code */ }
public boolean containsValue(java.lang.Object value) { /* compiled code */ }
public java.lang.Object get(java.lang.Object key) { /* compiled code */ }
public com.alibaba.fastjson.JSONObject getJSONObject(java.lang.String key) { /* compiled code */ }
public com.alibaba.fastjson.JSONArray getJSONArray(java.lang.String key) { /* compiled code */ }
public <T> T getObject(java.lang.String key, java.lang.Class<T> clazz) { /* compiled code */ }
public <T> T getObject(java.lang.String key, java.lang.reflect.Type type) { /* compiled code */ }
public <T> T getObject(java.lang.String key, com.alibaba.fastjson.TypeReference typeReference) { /* compiled code */ }
public java.lang.Boolean getBoolean(java.lang.String key) { /* compiled code */ }
public byte[] getBytes(java.lang.String key) { /* compiled code */ }
public boolean getBooleanValue(java.lang.String key) { /* compiled code */ }
public java.lang.Byte getByte(java.lang.String key) { /* compiled code */ }
public byte getByteValue(java.lang.String key) { /* compiled code */ }
public java.lang.Short getShort(java.lang.String key) { /* compiled code */ }
public short getShortValue(java.lang.String key) { /* compiled code */ }
public java.lang.Integer getInteger(java.lang.String key) { /* compiled code */ }
public int getIntValue(java.lang.String key) { /* compiled code */ }
public java.lang.Long getLong(java.lang.String key) { /* compiled code */ }
public long getLongValue(java.lang.String key) { /* compiled code */ }
public java.lang.Float getFloat(java.lang.String key) { /* compiled code */ }
public float getFloatValue(java.lang.String key) { /* compiled code */ }
public java.lang.Double getDouble(java.lang.String key) { /* compiled code */ }
public double getDoubleValue(java.lang.String key) { /* compiled code */ }
public java.math.BigDecimal getBigDecimal(java.lang.String key) { /* compiled code */ }
public java.math.BigInteger getBigInteger(java.lang.String key) { /* compiled code */ }
public java.lang.String getString(java.lang.String key) { /* compiled code */ }
public java.util.Date getDate(java.lang.String key) { /* compiled code */ }
public java.sql.Date getSqlDate(java.lang.String key) { /* compiled code */ }
public java.sql.Timestamp getTimestamp(java.lang.String key) { /* compiled code */ }
public java.lang.Object put(java.lang.String key, java.lang.Object value) { /* compiled code */ }
public com.alibaba.fastjson.JSONObject fluentPut(java.lang.String key, java.lang.Object value) { /* compiled code */ }
public void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m) { /* compiled code */ }
public com.alibaba.fastjson.JSONObject fluentPutAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> m) { /* compiled code */ }
public void clear() { /* compiled code */ }
public com.alibaba.fastjson.JSONObject fluentClear() { /* compiled code */ }
public java.lang.Object remove(java.lang.Object key) { /* compiled code */ }
public com.alibaba.fastjson.JSONObject fluentRemove(java.lang.Object key) { /* compiled code */ }
public java.util.Set<java.lang.String> keySet() { /* compiled code */ }
public java.util.Collection<java.lang.Object> values() { /* compiled code */ }
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet() { /* compiled code */ }
public java.lang.Object clone() { /* compiled code */ }
public boolean equals(java.lang.Object obj) { /* compiled code */ }
public int hashCode() { /* compiled code */ }
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.Throwable { /* compiled code */ }
public java.util.Map<java.lang.String,java.lang.Object> getInnerMap() { /* compiled code */ }
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException { /* compiled code */ }
public <T> T toJavaObject(java.lang.Class<T> clazz) { /* compiled code */ }
public <T> T toJavaObject(java.lang.Class<T> clazz, com.alibaba.fastjson.parser.ParserConfig config, int features) { /* compiled code */ }
static class SecureObjectInputStream extends java.io.ObjectInputStream {
static java.lang.reflect.Field[] fields;
static volatile boolean fields_error;
static void ensureFields() { /* compiled code */ }
public SecureObjectInputStream(java.io.ObjectInputStream in) throws java.io.IOException { /* compiled code */ }
protected java.lang.Class<?> resolveClass(java.io.ObjectStreamClass desc) throws java.io.IOException, java.lang.ClassNotFoundException { /* compiled code */ }
protected java.lang.Class<?> resolveProxyClass(java.lang.String[] interfaces) throws java.io.IOException, java.lang.ClassNotFoundException { /* compiled code */ }
protected void readStreamHeader() throws java.io.IOException, java.io.StreamCorruptedException { /* compiled code */ }
}
}
以上是关于JSONObjec的作用详解——Web网络系列学习笔记的主要内容,如果未能解决你的问题,请参考以下文章
@RequestMapping@GetMapping@PostMapping区别详解——Web网络系列学习笔记
@RequestMapping@GetMapping@PostMapping区别详解——Web网络系列学习笔记