导航:首页 > 编程语言 > json实例java

json实例java

发布时间:2023-07-22 11:18:10

A. 非常复杂的一个json串,怎么转成java对象

1、可借助开源组件FastJson或者Jackson。

2、自己动手

a、使用反射获取对象所有的setter方法以及对于所需的参数类型用map对应

b、循环上诉集合,依次使用反射调用set方法,并使用正则匹配json串中的值,可以使用class.getDeclareFileds();获取变量名,注意若参数是class类型需要反射为实例后填充,class类型匹配json串是也需要注意,因为可能存在嵌套对象中变量名一致的情况。展示部分代码如下:

3、综合分析还是建议使用中间件来进行映射。可参考Jackson示例

B. JAVA如何获取json字符串 最好有实例 感谢

//bean类
publicclassTet{
privateStringbuildingNum;//"E1",
privateStringclassName;//"1",
privateStringclassNum;//"通信15-1班",
privateStringcollege;//"电子信息工程学院",
privateStringdormNum;//"110",
privateStringgrade;//"2015",
privateStringisCommunist;//"0",
privateStringisDormLeader;//"1",
privateStringisStudentLeader;//"1",
privateStringmajor;//"通信工程",
privateStringphoneNum;//"13591991111",
privateStringstatus;//"1",
privateStringstudentId;//"1",
privateStringstudentName;//"张三",
privateStringteachername;//"张四"
publicStringgetBuildingNum(){
returnbuildingNum;
}
publicvoidsetBuildingNum(StringbuildingNum){
this.buildingNum=buildingNum;
}
publicStringgetClassName(){
returnclassName;
}
publicvoidsetClassName(StringclassName){
this.className=className;
}
publicStringgetClassNum(){
returnclassNum;
}
publicvoidsetClassNum(StringclassNum){
this.classNum=classNum;
}
publicStringgetCollege(){
returncollege;
}
publicvoidsetCollege(Stringcollege){
this.college=college;
}
publicStringgetDormNum(){
returndormNum;
}
publicvoidsetDormNum(StringdormNum){
this.dormNum=dormNum;
}
publicStringgetGrade(){
returngrade;
}
publicvoidsetGrade(Stringgrade){
this.grade=grade;
}
publicStringgetIsCommunist(){
returnisCommunist;
}
publicvoidsetIsCommunist(StringisCommunist){
this.isCommunist=isCommunist;
}
publicStringgetIsDormLeader(){
returnisDormLeader;
}
publicvoidsetIsDormLeader(StringisDormLeader){
this.isDormLeader=isDormLeader;
}
(){
returnisStudentLeader;
}
publicvoidsetIsStudentLeader(StringisStudentLeader){
this.isStudentLeader=isStudentLeader;
}
publicStringgetMajor(){
returnmajor;
}
publicvoidsetMajor(Stringmajor){
this.major=major;
}
publicStringgetPhoneNum(){
returnphoneNum;
}
publicvoidsetPhoneNum(StringphoneNum){
this.phoneNum=phoneNum;
}
publicStringgetStatus(){
returnstatus;
}
publicvoidsetStatus(Stringstatus){
this.status=status;
}
publicStringgetStudentId(){
returnstudentId;
}
publicvoidsetStudentId(StringstudentId){
this.studentId=studentId;
}
publicStringgetStudentName(){
returnstudentName;
}
publicvoidsetStudentName(StringstudentName){
this.studentName=studentName;
}
publicStringgetTeachername(){
returnteachername;
}
publicvoidsetTeachername(Stringteachername){
this.teachername=teachername;
}



}

//实现类
importnet.sf.json.JSONArray;
publicclassTest{
publicstaticvoidmain(String[]args){
/*
*做法建立一个bean类属性就为字符串的Key;
*/
//定义JSON串String类型
Stringa="[{"buildingNum":"E1","className":"1","classNum":"通信15-1班","college":"电子信息工程学院","dormNum":"110","grade":"2015","isCommunist":"0","isDormLeader":"1","isStudentLeader":"1","major":"通信工程","phoneNum":"13591991111","status":"1","studentId":"1","studentName":"张三","teachername":"张四"},{"buildingNum":"E1","className":"1","classNum":"通信15-1班","college":"电子信息工程学院","dormNum":"110","grade":"2015","isCommunist":"0","isDormLeader":"1","isStudentLeader":"1","major":"通信工程","phoneNum":"13591991111","status":"1","studentId":"2","studentName":"张1","teachername":"张四"},{"buildingNum":"E1","className":"1","classNum":"通信15-1班","college":"电子信息工程学院","dormNum":"110","grade":"2015","isCommunist":"0","isDormLeader":"1","isStudentLeader":"1","major":"通信工程","phoneNum":"13591991111","status":"1","studentId":"3","studentName":"张2","teachername":"张四"},{"buildingNum":"E1","className":"1","classNum":"通信15-1班","college":"电子信息工程学院","dormNum":"101","grade":"2015","isCommunist":"0","isDormLeader":"1","isStudentLeader":"1","major":"通信工程","phoneNum":"13591991111","status":"1","studentId":"4","
+""studentName":"张3","teachername":"张四"}]";
//转换成集合
List<Tet>list2=(List<Tet>)JSONArray.toList(JSONArray.fromObject(a),Tet.class);
for(Tett:list2){
//取出编号
System.out.println(t.getBuildingNum());
}
//转换成数组
Tet[]ss=(Tet[])JSONArray.toArray(JSONArray.fromObject(a),Tet.class);
for(Tett:ss){
//取出姓名
System.out.println(t.getTeachername());
}
}
}

C. java jSon实例编写

是这么个过程
1、在页面用js发出个ajax请求,请求类型最好写成json,建议使用jquery的ajax方法,省事。请求的路径要配在web.xml中
2、这个请求路径制定的是一个servlet,就是一个java类,继承自httpservlet。这个servlet里可以通过request对象获取到输入参数,根据输入参数和自己具体的逻辑拼出个字符串,当然是json格式的。然后输出。这个不会写说明你需要补一补java web开发的基础知识。
3、在页面的ajax请求的success方法中,直接可以获取到返回的json对象,然后就根据你的json格式处理吧。

$.ajax({
url: 'ajax/test',//这个是servlet请求路径
success: function(data) { //data就是servlet输出的json格式字符串,这里会自动转化为json对象
alert('Load was performed.');
}
});

D. java如何返回json格式

例如:
Student st1 = new Student(1, "dg", 18, new Date());
Student st2 = new Student(2, "dg", 18, new Date());
Student st3 = new Student(3, "dg", 18, new Date());
Student st4 = new Student(4, "dg", 18, new Date());
Student st5 = new Student(5, "dg", 18, new Date());
List li = new ArrayList();
JSONObject JO1 = new JSONObject(st1);
JSONObject JO2 = new JSONObject(st2);
JSONObject JO3 = new JSONObject(st3);
JSONObject JO4 = new JSONObject(st4);
JSONObject JO5 = new JSONObject(st5);
li.add(JO1);
li.add(JO2);
li.add(JO3);
li.add(JO4);
li.add(JO5);
JSONArray Ja = new JSONArray(li);
Map ma = new HashMap();
ma.put("Result", "OK");
ma.put("Records", Ja);
JSONObject js = new JSONObject(ma);
out.print(js);

返回结果:

{"Result":"OK","Records":[{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":1},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":2},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":3},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":4},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":5}]}

E. Java实现JSON多层遍历

JSONObject jsonObject = new JSONObject(s);
然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中
JSONObject jsonObject = new JSONObject(jsonString);
Iterator iterator = jsonObject.keys();while(iterator.hasNext()){
key = (String) iterator.next();
value = jsonObject.getString(key);
}

F. java中AJAX使用JSON的实例

我空间有一个例子:用Ajax实现多级联动下拉列表For JSP,地址:http://hi..com/tz666/blog/item/63f17ca342eb81b8cbefd019.html

G. java 怎么将List<List<Object>>类型转换成json类型,怎么读取json类型

java中将list对象转换成json类型,可以使用json拼接的方式,实例如下:

json:[{},{}]
Stringjson="[";
for(Objectobj:list){
json=json+"{"+obj+"}";
}
json=json+"]"
returnjson;
阅读全文

与json实例java相关的资料

热点内容
改解压格式 浏览:268
仿社交app源码 浏览:289
解压粘液模拟器英文版 浏览:671
看高铁票多少钱用什么app看 浏览:884
java接口返回值 浏览:89
奇迹文学APP为什么不能用了 浏览:1000
怎么用电脑玩手机版的我的世界服务器 浏览:230
linux设备权限 浏览:950
pdf转曲软件 浏览:149
云服务器怎么做nas 浏览:457
有什么可以提示快递的app 浏览:311
什么app的主题都是免费的 浏览:1002
se01加密路线和普通路线放 浏览:908
怎么用安卓系统手机测量长度 浏览:46
回调突破主图源码 浏览:238
mc生存都市服务器地址大全 浏览:92
unix网络编程环境搭建 浏览:963
pythonsocket高并发 浏览:257
python开发windowsgui 浏览:619
25减9的借位算法 浏览:269