方法一
我们可以根据前台的数据,在后台封装成一个vo的对象,里面存放的是变量的数组,那么 我们就能够得到传递的每一个数据
class student{
private String name[];
private int age[];
private String IdCard[];
省略set和get方法
}
方法二
这里我们以vue的方式进行叙述,先把前台数据转换成字符串
var student = JSON.stringify(this.studentList);
在把这个穿递给后台 那么后台的接收数据就可以这么写
对象:
class student{
private String name;
private int age;
private String IdCard;
省略set和get方法
}
Contriller:
public void getStudent(String studentList){
List<Student> list = JSONObject.parseArray(studentList,student.class);
}
这样 就能轻松的得到数组的list对象 而不需要对其前台数据进行封装成叔祖
工具类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.alibaba.fastjson;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
import java.io.IOException;
import java.io.NotActiveException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private final Map<String, Object> map;
public JSONObject() {
this(16, false);
}
public JSONObject(Map<String, Object> map) {
if (map == null) {
throw new IllegalArgumentException("map is null.");
} else {
this.map = map;
}
}
public JSONObject(boolean ordered) {
this(16, ordered);
}
public JSONObject(int initialCapacity) {
this(initialCapacity, false);
}
public JSONObject(int initialCapacity, boolean ordered) {
if (ordered) {
this.map = new LinkedHashMap(initialCapacity);
} else {
this.map = new HashMap(initialCapacity);
}
}
public int size() {
return this.map.size();
}
public boolean isEmpty() {
return this.map.isEmpty();
}
public boolean containsKey(Object key) {
return this.map.containsKey(key);
}
public boolean containsValue(Object value) {
return this.map.containsValue(value);
}
public Object get(Object key) {
Object val = this.map.get(key);
if (val == null && key instanceof Number) {
val = this.map.get(key.toString());
}
return val;
}
public JSONObject getJSONObject(String key) {
Object value = this.map.get(key);
if (value instanceof JSONObject) {
return (JSONObject)value;
} else if (value instanceof Map) {
return new JSONObject((Map)value);
} else {
return value instanceof String ? JSON.parseObject((String)value) : (JSONObject)toJSON(value);
}
}
public JSONArray getJSONArray(String key) {
Object value = this.map.get(key);
if (value instanceof JSONArray) {
return (JSONArray)value;
} else if (value instanceof List) {
return new JSONArray((List)value);
} else {
return value instanceof String ? (JSONArray)JSON.parse((String)value) : (JSONArray)toJSON(value);
}
}
public <T> T getObject(String key, Class<T> clazz) {
Object obj = this.map.get(key);
return TypeUtils.castToJavaBean(obj, clazz);
}
public <T> T getObject(String key, Type type) {
Object obj = this.map.get(key);
return TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance());
}
public <T> T getObject(String key, TypeReference typeReference) {
Object obj = this.map.get(key);
return typeReference == null ? obj : TypeUtils.cast(obj, typeReference.getType(), ParserConfig.getGlobalInstance());
}
public Boolean getBoolean(String key) {
Object value = this.get(key);
return value == null ? null : TypeUtils.castToBoolean(value);
}
public byte[] getBytes(String key) {
Object value = this.get(key);
return value == null ? null : TypeUtils.castToBytes(value);
}
public boolean getBooleanValue(String key) {
Object value = this.get(key);
Boolean booleanVal = TypeUtils.castToBoolean(value);
return booleanVal == null ? false : booleanVal;
}
public Byte getByte(String key) {
Object value = this.get(key);
return TypeUtils.castToByte(value);
}
public byte getByteValue(String key) {
Object value = this.get(key);
Byte byteVal = TypeUtils.castToByte(value);
return byteVal == null ? 0 : byteVal;
}
public Short getShort(String key) {
Object value = this.get(key);
return TypeUtils.castToShort(value);
}
public short getShortValue(String key) {
Object value = this.get(key);
Short shortVal = TypeUtils.castToShort(value);
return shortVal == null ? 0 : shortVal;
}
public Integer getInteger(String key) {
Object value = this.get(key);
return TypeUtils.castToInt(value);
}
public int getIntValue(String key) {
Object value = this.get(key);
Integer intVal = TypeUtils.castToInt(value);
return intVal == null ? 0 : intVal;
}
public Long getLong(String key) {
Object value = this.get(key);
return TypeUtils.castToLong(value);
}
public long getLongValue(String key) {
Object value = this.get(key);
Long longVal = TypeUtils.castToLong(value);
return longVal == null ? 0L : longVal;
}
public Float getFloat(String key) {
Object value = this.get(key);
return TypeUtils.castToFloat(value);
}
public float getFloatValue(String key) {
Object value = this.get(key);
Float floatValue = TypeUtils.castToFloat(value);
return floatValue == null ? 0.0F : floatValue;
}
public Double getDouble(String key) {
Object value = this.get(key);
return TypeUtils.castToDouble(value);
}
public double getDoubleValue(String key) {
Object value = this.get(key);
Double doubleValue = TypeUtils.castToDouble(value);
return doubleValue == null ? 0.0D : doubleValue;
}
public BigDecimal getBigDecimal(String key) {
Object value = this.get(key);
return TypeUtils.castToBigDecimal(value);
}
public BigInteger getBigInteger(String key) {
Object value = this.get(key);
return TypeUtils.castToBigInteger(value);
}
public String getString(String key) {
Object value = this.get(key);
return value == null ? null : value.toString();
}
public Date getDate(String key) {
Object value = this.get(key);
return TypeUtils.castToDate(value);
}
public java.sql.Date getSqlDate(String key) {
Object value = this.get(key);
return TypeUtils.castToSqlDate(value);
}
public Timestamp getTimestamp(String key) {
Object value = this.get(key);
return TypeUtils.castToTimestamp(value);
}
public Object put(String key, Object value) {
return this.map.put(key, value);
}
public JSONObject fluentPut(String key, Object value) {
this.map.put(key, value);
return this;
}
public void putAll(Map<? extends String, ? extends Object> m) {
this.map.putAll(m);
}
public JSONObject fluentPutAll(Map<? extends String, ? extends Object> m) {
this.map.putAll(m);
return this;
}
public void clear() {
this.map.clear();
}
public JSONObject fluentClear() {
this.map.clear();
return this;
}
public Object remove(Object key) {
return this.map.remove(key);
}
public JSONObject fluentRemove(Object key) {
this.map.remove(key);
return this;
}
public Set<String> keySet() {
return this.map.keySet();
}
public Collection<Object> values() {
return this.map.values();
}
public Set<Entry<String, Object>> entrySet() {
return this.map.entrySet();
}
public Object clone() {
return new JSONObject((Map)(this.map instanceof LinkedHashMap ? new LinkedHashMap(this.map) : new HashMap(this.map)));
}
public boolean equals(Object obj) {
return this.map.equals(obj);
}
public int hashCode() {
return this.map.hashCode();
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Class<?>[] parameterTypes = method.getParameterTypes();
Class returnType;
String name;
JSONField annotation;
if (parameterTypes.length == 1) {
if (method.getName().equals("equals")) {
return this.equals(args[0]);
} else {
returnType = method.getReturnType();
if (returnType != Void.TYPE) {
throw new JSONException("illegal setter");
} else {
name = null;
annotation = (JSONField)TypeUtils.getAnnotation(method, JSONField.class);
if (annotation != null && annotation.name().length() != 0) {
name = annotation.name();
}
if (name == null) {
name = method.getName();
if (!name.startsWith("set")) {
throw new JSONException("illegal setter");
}
name = name.substring(3);
if (name.length() == 0) {
throw new JSONException("illegal setter");
}
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
this.map.put(name, args[0]);
return null;
}
}
} else if (parameterTypes.length == 0) {
returnType = method.getReturnType();
if (returnType == Void.TYPE) {
throw new JSONException("illegal getter");
} else {
name = null;
annotation = (JSONField)TypeUtils.getAnnotation(method, JSONField.class);
if (annotation != null && annotation.name().length() != 0) {
name = annotation.name();
}
if (name == null) {
name = method.getName();
if (name.startsWith("get")) {
name = name.substring(3);
if (name.length() == 0) {
throw new JSONException("illegal getter");
}
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
} else {
if (!name.startsWith("is")) {
if (name.startsWith("hashCode")) {
return this.hashCode();
}
if (name.startsWith("toString")) {
return this.toString();
}
throw new JSONException("illegal getter");
}
name = name.substring(2);
if (name.length() == 0) {
throw new JSONException("illegal getter");
}
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
}
Object value = this.map.get(name);
return TypeUtils.cast(value, method.getGenericReturnType(), ParserConfig.getGlobalInstance());
}
} else {
throw new UnsupportedOperationException(method.toGenericString());
}
}
public Map<String, Object> getInnerMap() {
return this.map;
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
JSONObject.SecureObjectInputStream.ensureFields();
if (JSONObject.SecureObjectInputStream.fields != null && !JSONObject.SecureObjectInputStream.fields_error) {
JSONObject.SecureObjectInputStream secIn = new JSONObject.SecureObjectInputStream(in);
try {
secIn.defaultReadObject();
return;
} catch (NotActiveException var6) {
;
}
}
in.defaultReadObject();
Iterator var7 = this.map.entrySet().iterator();
while(var7.hasNext()) {
Entry entry = (Entry)var7.next();
Object key = entry.getKey();
if (key != null) {
ParserConfig.global.checkAutoType(key.getClass());
}
Object value = entry.getValue();
if (value != null) {
ParserConfig.global.checkAutoType(value.getClass());
}
}
}
public <T> T toJavaObject(Class<T> clazz) {
if (clazz != Map.class && clazz != JSONObject.class && clazz != JSON.class) {
return clazz == Object.class && !this.containsKey(JSON.DEFAULT_TYPE_KEY) ? this : TypeUtils.castToJavaBean(this, clazz, ParserConfig.getGlobalInstance());
} else {
return this;
}
}
public <T> T toJavaObject(Class<T> clazz, ParserConfig config, int features) {
if (clazz == Map.class) {
return this;
} else {
return clazz == Object.class && !this.containsKey(JSON.DEFAULT_TYPE_KEY) ? this : TypeUtils.castToJavaBean(this, clazz, config);
}
}
static class SecureObjectInputStream extends ObjectInputStream {
static Field[] fields;
static volatile boolean fields_error;
static void ensureFields() {
if (fields == null && !fields_error) {
try {
Field[] declaredFields = ObjectInputStream.class.getDeclaredFields();
String[] fieldnames = new String[]{"bin", "passHandle", "handles", "curContext"};
Field[] array = new Field[fieldnames.length];
for(int i = 0; i < fieldnames.length; ++i) {
Field field = TypeUtils.getField(ObjectInputStream.class, fieldnames[i], declaredFields);
field.setAccessible(true);
array[i] = field;
}
fields = array;
} catch (Throwable var5) {
fields_error = true;
}
}
}
public SecureObjectInputStream(ObjectInputStream in) throws IOException {
super(in);
try {
for(int i = 0; i < fields.length; ++i) {
Field field = fields[i];
Object value = field.get(in);
field.set(this, value);
}
} catch (IllegalAccessException var5) {
fields_error = true;
}
}
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
String name = desc.getName();
if (name.length() > 2) {
int index = name.lastIndexOf(91);
if (index != -1) {
name = name.substring(index + 1);
}
if (name.length() > 2 && name.charAt(0) == 'L' && name.charAt(name.length() - 1) == ';') {
name = name.substring(1, name.length() - 1);
}
ParserConfig.global.checkAutoType(name, (Class)null, Feature.SupportAutoType.mask);
}
return super.resolveClass(desc);
}
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
String[] var2 = interfaces;
int var3 = interfaces.length;
for(int var4 = 0; var4 < var3; ++var4) {
String interfacename = var2[var4];
ParserConfig.global.checkAutoType(interfacename, (Class)null);
}
return super.resolveProxyClass(interfaces);
}
protected void readStreamHeader() throws IOException, StreamCorruptedException {
}
}
}