本文目錄一覽:
- 1、Python請求java中文編碼問題
- 2、java中json怎麼運用?
- 3、java後台怎樣傳json格式的數據
- 4、java中怎麼把數據轉換成Json數據
- 5、java調用python時傳遞的參數問題?
Python請求java中文編碼問題
首先 全部使用 「utf-8」編碼
然後 分別在 java 和 python 中使用 json.dumps() 和 json.loads() 來處理 json數據。這樣肯定不會出錯的。
java中使用:import net.sf.json.JSONArray; import net.sf.json.JSONObject;
python中使用:import json
java中json怎麼運用?
json一般都是配合ajax一起使用的 我做做過的小例子 粘給你 你可以研究一下
js部分
//獲取卡的金額
function get_money(){
var str=document.getElementById(“pk_card_type”).value;
//alert(str);
var url = ‘/member_h.do’;
var pars = ‘method=getMoney’;
pars+=’pk_card_type=’+str;
var ajax = new Ajax.Request(
url,
{method:’post’,parameters:pars,onComplete:show_money}
);
}
//回調函數 寫入卡的金額
function show_money(dataResponse)
{
var data = eval(‘(‘ + dataResponse.responseText + ‘)’);
var price=0;
price=data.price;
var collection_fees=0;
collection_fees=data.collection_fees;
document.getElementById(“recharge”).value=price;
document.getElementById(“collection_fees”).value=collection_fees;
}
action部分
public ActionForward getMoney(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType(“text/html; charset=utf-8”);
try {
IElementaryFileService ggsv = new ElementaryFileService();
String pk_card_type = request.getParameter(“pk_card_type”);
Card_TypeVO ctvo=new Card_TypeVO();
ctvo=ggsv.queryByPK(Card_TypeVO.class, pk_card_type);
PrintWriter out = response.getWriter();
// 這裡的數據拼裝一般是從資料庫查詢來的
JSONObject jsonObject = new JSONObject();
if(ctvo!=null){
jsonObject.put(“price”, ctvo.getCard_money());
jsonObject.put(“collection_fees”, ctvo.getCash());
}else{
jsonObject.put(“price”, 0);
jsonObject.put(“collection_fees”, 0);
}
out.print(jsonObject.toString());
out.flush();
out.close();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
java後台怎樣傳json格式的數據
通過 JSONObject類就可以了
首先 你把這幾個包 下下來 放到你項目。如果有就不要下了:
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar
像你這種是數據形式 就通過 JSONArray 如:
JSONArray datasJson = JSONArray.fromObject(datas);最好把datas toString 一下
java中怎麼把數據轉換成Json數據
搜json-lib.jar
這個包的例子:
JSONObject obj = new JSONObject();
obj.put(“name”, “kotomi”);
obj.toString();
得到:{“name”:”kotomi”}
也可以吧自己定義的實體轉,如
JSONObject.fromObject(xxx);
xxx是你自己定義的實體,他會吧xxx里提供了getter的都轉成json
java調用python時傳遞的參數問題?
需要用到需要用到jython.jar
java example:
public static void main(String[] args) {
//定義參數
String[] args2 = {“arg1″,”arg2”};
//設置參數
PythonInterpreter.initialize(null, null, args2);
PythonInterpreter interpreter = new PythonInterpreter();
//執行
interpreter.execfile(“E:\\jython.py”);
System.out.println(“———-run over!———-“);
}
python的程序:
#!/bin/env python
import time
import sys
argCount = len(sys.argv)
print(‘before sleep’)
time.sleep(5);
print(‘after sleep’)
for str in sys.argv:
print(str)
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130336.html