Java 在jsp页面中使用GSON
我尝试使用类似JSP的格式,使用以下代码序列化JSON中的对象:Java 在jsp页面中使用GSON,java,json,gson,Java,Json,Gson,我尝试使用类似JSP的格式,使用以下代码序列化JSON中的对象: ArrayList<AccountBean> al = new ArrayList<AccountBean>(); al = vc.getAccountName(); int i=0; out.print("["); while(i<al.size()){ Gson gson = new GsonBuilder().setPrettyPrinting().create(); out.p
ArrayList<AccountBean> al = new ArrayList<AccountBean>();
al = vc.getAccountName();
int i=0;
out.print("[");
while(i<al.size()){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
out.print("{ID"+al.get(i).getAno()+":name"+al.get(i).getAccount_name()+"},");
i++;
}
out.print("]");
但我的要求是这样的:
[{ID1:nameEquity Share Capitals},{ID2:nameCalls In Arear},]
[{"ID1":"nameEquity Share Capitals"},{"ID2":"nameCalls In Arear"}]
使用”
打开/关闭字符串,使用“
包装json键/值
否则你可以这样做
out.print("{\"ID"+al.get(i).getAno()+"\":\"name"+al.get(i).getAccount_name()+"\"},")
使用\“
不管怎样,你试过这个吗
String json = gson.toJson(al)
查看此处了解更多信息:最好的方法是使用自定义序列化程序,如果您想深入了解,我可以编辑此答案 然而,由于您对JSON和Gson非常陌生,我将用这个简单的代码来回答,您可以在IDE中粘贴并尝试。我只是将youbean“转换”成一个映射,然后使用Gson进行序列化
package stackoverflow.questions;
import java.util.*;
import com.google.gson.Gson;
public class Q20323412 {
public static class AccountBean{
Integer _id;
String _name;
public String getAccount_name(){
return _name;
}
public Integer getAno(){
// what a weird name, in italian for this method..
return _id;
}
public AccountBean(Integer id, String name){
_id = id;
_name = name;
}
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<AccountBean> al = new ArrayList<AccountBean>();
al.add(new AccountBean(1, "Equity Share Capitals"));
al.add(new AccountBean(2, "Calls In Arear"));
ArrayList<Map> al2 = new ArrayList<>();
for(AccountBean account : al){
HashMap hm = new HashMap();
hm.put("ID"+ account.getAno(), "name"+account.getAccount_name());
al2.add(hm);
}
Gson g = new Gson();
System.out.println(g.toJson(al2));
}
}
package stackoverflow.questions;
导入java.util.*;
导入com.google.gson.gson;
公开课Q20323412{
公共静态类AccountBean{
整数_id;
字符串\u名称;
公共字符串getAccount_name(){
返回_name;
}
公共整数getAno(){
//用意大利语来说,这是一个多么奇怪的名字。。
返回_id;
}
公共AccountBean(整数id、字符串名称){
_id=id;
_名称=名称;
}
}
/**
*@param args
*/
公共静态void main(字符串[]args){
ArrayList al=新的ArrayList();
新增(新账户(1,“股本”);
新增(新账户Bean(2,“区域内呼叫”);
ArrayList al2=新的ArrayList();
for(AccountBean帐户:al){
HashMap hm=新的HashMap();
hm.put(“ID”+account.getAno(),“name”+account.getAccount_name());
al2.add(hm);
}
Gson g=新的Gson();
System.out.println(g.toJson(al2));
}
}
因为您没有发布bean,所以我发明了一个具有类似于您的特性的bean。从Gson获得这种序列化的方法真的没有更干净的方法(从一个bean数组到一个系列再到键,这真的是一种非标准的需要)
package stackoverflow.questions;
import java.util.*;
import com.google.gson.Gson;
public class Q20323412 {
public static class AccountBean{
Integer _id;
String _name;
public String getAccount_name(){
return _name;
}
public Integer getAno(){
// what a weird name, in italian for this method..
return _id;
}
public AccountBean(Integer id, String name){
_id = id;
_name = name;
}
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<AccountBean> al = new ArrayList<AccountBean>();
al.add(new AccountBean(1, "Equity Share Capitals"));
al.add(new AccountBean(2, "Calls In Arear"));
ArrayList<Map> al2 = new ArrayList<>();
for(AccountBean account : al){
HashMap hm = new HashMap();
hm.put("ID"+ account.getAno(), "name"+account.getAccount_name());
al2.add(hm);
}
Gson g = new Gson();
System.out.println(g.toJson(al2));
}
}