json教程 java(java中json格式转换有哪些方法)
本文目录
java中json格式转换有哪些方法
用自带的解析工具
package cn.edu.bzu.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class Read {
public static void main(String args){
JsonParser parse =new JsonParser(); //创建json解析器
try {
JsonObject json=(JsonObject) parse.parse(new FileReader("weather.json")); //创建jsonObject对象
System.out.println("resultcode:"+json.get("resultcode").getAsInt()); //将json数据转为为int型的数据
System.out.println("reason:"+json.get("reason").getAsString()); //将json数据转为为String型的数据
JsonObject result=json.get("result").getAsJsonObject();
JsonObject today=result.get("today").getAsJsonObject();
System.out.println("temperature:"+today.get("temperature").getAsString());
System.out.println("weather:"+today.get("weather").getAsString());
} catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
java 怎么将List>类型转换成json类型,怎么读取json类型
java中将list对象转换成json类型,可以使用json拼接的方式,实例如下:
json:
String json="[";
for(Object obj:list){
json=json+"{"+obj+"}";
}
json=json+"]"
return 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(dataResp***e)
{
var data = eval(’(’ + dataResp***e.resp***eText + ’)’);
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, HttpServletResp***e resp***e) {
resp***e.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 = resp***e.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实现list转换成json格式
首先要导入json包
import net.sf.json.JSONObject;
import net.sf.json.JSON;
list转成json数组:
JSONArray arr = JSONArray.formObject(cc);
再讲json数组放入json对象:
JSONObject json = new JSONObject();
json.put("obj",arr);
要下载第三方的jar包,你自己找找啊
更多文章:
bigdecimal 除法(java中 BigDecimal的类型的除法)
2026年3月27日 17:40
length函数c(C语言自定义一个函数int length(char *s),函数返回字符串s的长度)
2026年3月27日 17:20
pictureselector(图片选择器:PictureSelector)
2026年3月27日 17:00
自学python经历(零基础零经验自学Python,到精通Python要多久啊)
2026年3月27日 16:40
null**rawl荒野乱斗下载(哪个软件可以下载荒野乱斗破解版)
2026年3月27日 16:20





