获取前端传过来的json对象
代码如下
public JSONObject getRequestContent(>
反射机制。
把a换成你的Object对象就行了
import javalangreflectField;
public class Test {
public static void main(String[] args) throws Exception {
A a = new A();
aname = "test";
aage = 20;
Field[] fields = agetClass()getFields();
for (int i = 0; i < fieldslength; i++) {
Field field = fields[i];
fieldsetAccessible(true);//忽略访问权限,私有的也可以访问
Systemoutprintln(fieldgetName() + " = " + fieldget(a));
}
}
}
class A {
public String name = "";
public int age = 0;
}
Person person = new Person("1","gson",1);
Gson gson = new Gson();
//这里将javabean转化成json字符串
String jsonString = gsontoJson(person);
Systemoutprintln(jsonString);
//这里将json字符串转化成javabean对象,
person = gsonfromJson(jsonString,Personclass);
将json字符串转化成List<JavaBean>对象
Person person1 = new Person("1","gson1",1);
Person person2 = new Person("2","gson2",2);
List<Person> persons = new ArrayList<Person>();
personsadd(person1);
personsadd(person2);
Gson gson = new Gson();
//这里将lsit<javabean>转化成json字符串
String jsonString = gsontoJson(persons);
//解析json字符串
List<Person> persons2 = gsonfromJson(jsonString, new TypeToken<List<Person>>(){}getType());
将json字符串转化成List<String>对象
List<String> list = new ArrayList<String>();
listadd("gson1");
listadd("gson2");
listadd("gson3");
Gson gson = new Gson();
String jsonString = gsontoJson(list);
Systemoutprintln("json字符串:"+jsonString);
//解析json字符串
List<String> list2 = gsonfromJson(jsonString, new TypeToken<List<String>>(){}getType());
将json字符串转化成List<Map<String,Object>>对象
Map<String,Object> map = new HashMap<String,Object>();
mapput("key1", "value1");
mapput("key2", "value2");
Map<String,Object> map2 = new HashMap<String,Object>();
map2put("key1", 1);
map2put("key2", 2);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
listadd(map);
listadd(map2);
Gson gson = new Gson();
String jsonString = gsontoJson(list);
Systemoutprintln("json字符串:"+jsonString);
//解析json字符串
List<Map<String,Object>> list2 = gsonfromJson(jsonString, new TypeToken<List<Map<String,Object>>>(){}getType());
以上就是关于如何获取前端传过来的json对象全部的内容,包括:如何获取前端传过来的json对象、object怎么取值、如何获取一个Object中的数据。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)