導航:首頁 > 編程語言 > java返回js

java返回js

發布時間:2022-12-11 05:21:23

A. java 訪問一個介面得到介面返回JSON,步驟是怎麼做的

java中的介面是一種特殊的類,使用關鍵字interface創建。介面功能完全實現後,可以打成jar包,提供給其他公司使用。
要返回json格式數據,可以把介面中抽象方法的返回值類型規定為JSONObject或JSONString類型。這樣當其他公司調用時,得到的數據就是json數據了。
另外,以jar形式提供的介面,可以通過反編譯得到你的源碼,如果你不希望開源,就要加密了。

B. web環境下,servlet如何設計java介面接收json,並將處理結果按json格式返回

JSONObject jsonObject = new JSONObject();\x0d\x0aMap map = request.getParameterMap();\x0d\x0aIterator it = map.keySet().iterator();\x0d\x0awhile(it.hasNext()){\x0d\x0aString key = (String)it.next();\x0d\x0aString[] values = (String[])map.get(key);\x0d\x0ajsonObject.accumulate(key, values[0]);\x0d\x0a}\x0d\x0a\x0d\x0aString name = jsonObject.getString("userName"); //返回從前台接受的用戶名\x0d\x0aSystem.out.println(name); //輸出用戶名\x0d\x0a\x0d\x0ajsonObject.clear(); // 清空jsonObjec中的數據\x0d\x0ajsonObject.put("love" , "足球"); //將足球賦給love這個變數名\x0d\x0aout.print(jsonObject); //返回json格式的數據

C. 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}]}

D. java如何返回json格式

處理基本的java對象使用JSONObject類,用法大體如下:

public void testMap(){

Map<String,Object> map = new HashMap<String,Object>();

map.put("name", "qiu");

map.put("password", "123");

map.put("address", "china");

User user = new User();

user.setUserName("qiuqiu");

user.setPassword("123456");

user.getTels().add("1234444556677");

user.getTels().add("6893493458585");

map.put("user", user);

JSONObject json = new JSONObject(map);

System.out.println(json.toString());

}

閱讀全文

與java返回js相關的資料

熱點內容
磁力計校正演算法 瀏覽:491
解壓縮後變小了 瀏覽:957
智友文件夾 瀏覽:81
android操作系統開發的操作系統 瀏覽:478
原神手機怎麼改b站伺服器 瀏覽:296
樁基箍筋加密區高度規范 瀏覽:91
手機櫻花動漫app怎麼用 瀏覽:382
php科學計數法轉換 瀏覽:642
sip認證演算法 瀏覽:785
androidapp卡頓原因 瀏覽:905
25編程器電路 瀏覽:849
安卓九是什麼東西 瀏覽:939
隱藏nodejs命令行窗口 瀏覽:62
人體與寫生素描pdf 瀏覽:883
java集合性能 瀏覽:143
單片機三線通信 瀏覽:209
崑山ug編程培訓學費 瀏覽:628
黃色app怎麼盈利的 瀏覽:957
怎麼修改linux密碼 瀏覽:703
國家發展中心app長什麼樣子 瀏覽:242