導航:首頁 > 編程語言 > javajsonjar

javajsonjar

發布時間:2023-01-04 02:32:19

1. 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;
}
}

2. Java實體類轉換成json字元串的方法 最好告訴我需要哪些jar

你用的是strtus2吧
需要的jar包:commons-lang-2.4.jar;
json-lib-2.3-jdk13.jar;
jsonplugin-0[1].32.jar;
ezmorph-1.0.2.jar;
commons-beanutils-1.7.0.jar;
在struts.xml中<package name="XXXX" extends="json-default"> extends要擴展json-default

http://blog.csdn.net/agxy08xx1mxx/article/details/6962303這篇文章可以看看,上面講的很詳細

3. 要在java里使用json,要用到的jar包怎麼導入

先把jar包放在項目下面,最好是新建一個專門放包的文件夾

4. 如何java解析json數組

工具/原料

5. Java解析json數據

一、 JSON (JavaScript Object Notation)一種簡單的數據格式,比xml更輕巧。
Json建構於兩種結構:
1、「名稱/值」對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。 如:
{
「name」:」jackson」,
「age」:100
}

2、值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)如:
{
「students」:
[
{「name」:」jackson」,「age」:100},
{「name」:」michael」,」age」:51}
]
}
二、java解析JSON步驟
A、伺服器端將數據轉換成json字元串
首先、伺服器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:http://json-lib.sourceforge.net/)

然後將數據轉為json字元串,核心函數是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
B、客戶端將json字元串轉換為相應的javaBean
1、客戶端獲取json字元串(因為android項目中已經集成了json的jar包所以這里無需導入)
public class HttpUtil
{

public static String getJsonContent(String urlStr)
{
try
{// 獲取HttpURLConnection連接對象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 獲取相應碼
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 將輸入流轉移到內存輸出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 將內存流轉換為字元串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、獲取javaBean
public static Person getPerson(String jsonStr)
{
Person person = new Person();
try
{// 將json字元串轉換為json對象
JSONObject jsonObj = new JSONObject(jsonStr);
// 得到指定json key對象的value對象
JSONObject personObj = jsonObj.getJSONObject("person");
// 獲取之對象的所有屬性
person.setId(personObj.getInt("id"));
person.setName(personObj.getString("name"));
person.setAddress(personObj.getString("address"));
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return person;
}

public static List<Person> getPersons(String jsonStr)
{
List<Person> list = new ArrayList<Person>();

JSONObject jsonObj;
try
{// 將json字元串轉換為json對象
jsonObj = new JSONObject(jsonStr);
// 得到指定json key對象的value對象
JSONArray personList = jsonObj.getJSONArray("persons");
// 遍歷jsonArray
for (int i = 0; i < personList.length(); i++)
{
// 獲取每一個json對象
JSONObject jsonItem = personList.getJSONObject(i);
// 獲取每一個json對象的值
Person person = new Person();
person.setId(jsonItem.getInt("id"));
person.setName(jsonItem.getString("name"));
person.setAddress(jsonItem.getString("address"));
list.add(person);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

6. java怎麼處理json格式數據

1、通過谷歌的Gson來進行解析:

json數據:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};

2、通過json-org.jar包進行解析:

json數據:sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{surveyid":"2","surveyname":"C"}]};

7. java怎麼得到json中的數據

如果不是Android開發環境的話,首先需要引入處理JSON數據的包:json-lib-2.2.3-jdk15.jar


Java樣常式序如下:

importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;

publicclassDoJSON{
publicstaticvoidmain(String[]args){
JSONArrayemployees=newJSONArray(); //JSON數組
JSONObjectemployee=newJSONObject(); //JSON對象

employee.put("firstName","Bill"); //按「鍵-值」對形式存儲數據到JSON對象中
employee.put("lastName","Gates");
employees.add(employee); //將JSON對象加入到JSON數組中

employee.put("firstName","George");
employee.put("lastName","Bush");
employees.add(employee);

employee.put("firstName","Thomas");
employee.put("lastName","Carter");
employees.add(employee);

System.out.println(employees.toString());
for(inti=0;i<employees.size();i++){
JSONObjectemp=employees.getJSONObject(i);
System.out.println(emp.toString());
System.out.println("FirstName: "+emp.get("firstName"));
System.out.println("LastName: "+emp.get("lastName"));
}
}
}


運行效果:

[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]

{"firstName":"Bill","lastName":"Gates"}

FirstName : Bill

LastName : Gates

{"firstName":"George","lastName":"Bush"}

FirstName : George

LastName : Bush

{"firstName":"Thomas","lastName":"Carter"}

FirstName : Thomas

LastName : Carter

8. java使用json需要哪些jar包

JsonObject Gson兩大開源框架非常簡單一行代碼實現json與java相互轉換

String json = new Gson().toJson(object);

閱讀全文

與javajsonjar相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:755
蘋果郵件無法連接伺服器地址 瀏覽:958
phpffmpeg轉碼 瀏覽:669
長沙好玩的解壓項目 瀏覽:140
專屬學情分析報告是什麼app 瀏覽:562
php工程部署 瀏覽:831
android全屏透明 瀏覽:730
阿里雲伺服器已開通怎麼辦 瀏覽:801
光遇為什麼登錄時伺服器已滿 瀏覽:300
PDF分析 瀏覽:482
h3c光纖全工半全工設置命令 瀏覽:140
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:681
如何取消命令方塊指令 瀏覽:347
風翼app為什麼進不去了 瀏覽:776
im4java壓縮圖片 瀏覽:360
數據查詢網站源碼 瀏覽:148
伊克塞爾文檔怎麼進行加密 瀏覽:888
app轉賬是什麼 瀏覽:162