json转换成list对象(如何把json树字符串转换为java list对象)

本文目录
如何把json树字符串转换为java list对象
用JSONObject这个类库
用谷歌给我们提供的Gson
使用方式:
JSONObject json= new JSONObject(json);
json.getString(key);
Object obj = new Gson().fromJson(json,Object.class);
将这个object替换成你的javaBean
如何把JsonObject转换为list
使用json-lib 包来进行java 对象和json 之间的相互转换
JSONArray jsonArray=JSONArray.fromObject(list);//将list 转换为json 数组
response.setContentType( application/json );//设置返回的数据为json 对象
如果是jsp 的页面直接用out.print(jsonArray);注意统一编码
servlet 或者action 中都用response 对象getWriter.print(jsonArray)
那你的请求地址是jsp 页面还是servlet 还是action。请求哪个就写哪个url 就是了
为了简单都用jquery 来进行ajax
$.getJSON( 你的 url 地址 ,function(data){//回调函数接收返回的 json 格式数据data});
json字符串怎么转化为list
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
public class Test {
public static void main(String args) {
String json = "{\"name\":\"reiz\"}";
JSONObject jsonObj = JSONObject.fromObject(json);
String name = jsonObj.getString("name");
jsonObj.put("initial", name.substring(0, 1).toUpperCase());
String { "JavaScript", "Skiing", "Apple Pie" };
jsonObj.put("likes", likes);
Map 《String, String》 ingredients = new HashMap 《String, String》();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients",ingredients);
System.out.println(jsonObj);
}
}
//这是使用org.json的程序:
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String args) throws JSONException {
String json = "{\"name\":\"reiz\"}";
JSONObject jsonObj = new JSONObject(json);
String name = jsonObj.getString("name");
jsonObj.put("initial", name.substring(0, 1).toUpperCase());
String { "JavaScript", "Skiing", "Apple Pie" };
jsonObj.put("likes", likes);
Map 《String, String》 ingredients = new HashMap 《String, String》();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients", ingredients);
System.out.println(jsonObj);
System.out.println(jsonObj);
}
}
//两者的使用几乎是相同的,但org.json比json-lib要轻量得多,前者没有任何依赖,而后者要依赖ezmorph和commons的lang、logging、beanutils、collections等//组件。

更多文章:
unknown hard error是什么意思(unknown hard error是什么意思)
2025年9月1日 17:15
getconsolewindow(C语言 怎么使控制台窗口大小固定不变)
2025年6月6日 16:00
建站大师排名表2021(国内二次元手游2021年流水数据揭晓,最大赢家是谁)
2026年1月2日 02:15
html怎么在表格里加表格(如何在html单元格中插入表格并铺满单元格)
2026年5月14日 03:45
编号非自动列表是什么意思(word中项目符号,编号,多级列表什么区别及各用在什么场合)
2026年8月17日 05:15
oracle10客户端安装(Oracle Database 10g在windows上安装错误怎么解决)
2026年8月25日 13:00
nip和pluck的区别?百万英镑中,有这么一句,just like an englishman ,you see ,plu
2026年9月14日 14:15














