導航:首頁 > 編程語言 > 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相關的資料

熱點內容
沒加密招標文件 瀏覽:257
命令來自剃頭的用英語怎麼說 瀏覽:765
什麼app不花一分錢買東西 瀏覽:373
布林四線指標源碼 瀏覽:968
單片機的控制板 瀏覽:218
襄陽軟體編程 瀏覽:841
sshpass命令 瀏覽:106
logo伺服器怎麼下載 瀏覽:508
如何ftp連接伺服器 瀏覽:674
creo自動編程 瀏覽:161
雲伺服器在電腦怎麼開 瀏覽:432
ipad相冊如何在文件夾中建文件夾 瀏覽:621
和家親這個app有什麼用 瀏覽:575
什麼app裡面有種樹打折 瀏覽:374
編程外掛入門教學 瀏覽:974
pdf黑白轉彩色 瀏覽:725
英國投資加密貨幣嗎 瀏覽:887
看完程序員那麼可愛後的感受 瀏覽:131
廣播在什麼APP能聽 瀏覽:678
阿克曼小車連接什麼app 瀏覽:773