如何根据json格式生成java bean对象 20
1个回答
展开全部
,提问者: 这是我之前写的一个例子,贴出来分享一下!js 拼接json<pre t="code" l="java">function jsonData(){
var jsonStr="[";
jsonStr+="{";
jsonStr+="\"prizelevel\":"+"\""+prizelevel+"\"";
jsonStr+=",";
jsonStr+="\"prizetype\":"+"\""+prizetype+"\"";
jsonStr+=",";
jsonStr+="\"prizename\":"+"\""+prizename+"\"";
jsonStr+=",";
jsonStr+="\"prizenum\":"+"\""+prizenum+"\"";
jsonStr+=",";
jsonStr+="\"prizeimgurl\":"+"\""+prizeimgurl+"\"";
jsonStr+=",";
jsonStr+="\"prizecode\":"+"\""+prizecode+"\"";
jsonStr+=",";
jsonStr+="\"prizeval\":"+"\""+prizeval+"\"";
jsonStr+=",";
jsonStr+="\"prizevalid\":"+"\""+prizevalid+"\"";
jsonStr+="}";
jsonStr+="]";
return jsonStr;//关键在于转换。
}
function add(){
var prize = jsonData();
if(prize==false){
return;
}
$.post( url ,
{interactPrizeAll:prize},
function(data){ }
);//此处post请求后台!
}action 拆分 json<pre t="code" l="java">//拆分字符串
System.out.println(interactPrizeAll);
//奖品
Gson gson = new Gson();
interactPrize =new InteractPrize();
//gson用泛型 转List数组 多个对象
interactPrizeList = gson.fromJson(interactPrizeAll, new TypeToken<List<InteractPrize>>(){}.getType()); //TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换
for(int i = 0; i < interactPrizeList.size(); i++)
{
interactPrize = interactPrizeList.get(i);//获取每一个对象
}
var jsonStr="[";
jsonStr+="{";
jsonStr+="\"prizelevel\":"+"\""+prizelevel+"\"";
jsonStr+=",";
jsonStr+="\"prizetype\":"+"\""+prizetype+"\"";
jsonStr+=",";
jsonStr+="\"prizename\":"+"\""+prizename+"\"";
jsonStr+=",";
jsonStr+="\"prizenum\":"+"\""+prizenum+"\"";
jsonStr+=",";
jsonStr+="\"prizeimgurl\":"+"\""+prizeimgurl+"\"";
jsonStr+=",";
jsonStr+="\"prizecode\":"+"\""+prizecode+"\"";
jsonStr+=",";
jsonStr+="\"prizeval\":"+"\""+prizeval+"\"";
jsonStr+=",";
jsonStr+="\"prizevalid\":"+"\""+prizevalid+"\"";
jsonStr+="}";
jsonStr+="]";
return jsonStr;//关键在于转换。
}
function add(){
var prize = jsonData();
if(prize==false){
return;
}
$.post( url ,
{interactPrizeAll:prize},
function(data){ }
);//此处post请求后台!
}action 拆分 json<pre t="code" l="java">//拆分字符串
System.out.println(interactPrizeAll);
//奖品
Gson gson = new Gson();
interactPrize =new InteractPrize();
//gson用泛型 转List数组 多个对象
interactPrizeList = gson.fromJson(interactPrizeAll, new TypeToken<List<InteractPrize>>(){}.getType()); //TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换
for(int i = 0; i < interactPrizeList.size(); i++)
{
interactPrize = interactPrizeList.get(i);//获取每一个对象
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询