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

熱點內容
什麼是適合自己的app 瀏覽:451
雲伺服器怎麼補漏洞 瀏覽:176
如何看自己的安卓是什麼等級 瀏覽:633
被丁磊挖走的程序員 瀏覽:19
gsk980ta編程 瀏覽:507
單片機離線 瀏覽:84
解壓助眠敲擊外國長指甲 瀏覽:421
3次元編程 瀏覽:803
夫妻傳送英文命令 瀏覽:316
程序員題代做 瀏覽:950
戴爾伺服器怎麼擴大內存 瀏覽:641
解壓包密碼消失 瀏覽:135
昆侖通態觸摸屏用戶窗加密 瀏覽:564
農信app怎麼看日額 瀏覽:865
iphone備忘錄包含視頻無法加密 瀏覽:283
抖音快手紅包源碼 瀏覽:137
程序員資源分析 瀏覽:586
linux交換分區大小 瀏覽:922
mt怎麼編譯 瀏覽:337
qq郵箱打開pdf 瀏覽:461