json数据后台处理

23 篇文章 0 订阅
18 篇文章 0 订阅
JAVA解析JSON问题,怎么解析,急!!

String jsonstr = "[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Thumbnail": {
"Url": "和谐xxx",
"Height": 125,
"Width": "100"
}
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Thumbnail": {
"Url": "和谐xxx",
"Height": 125,
"Width": "100"
}

}
]"


我想解析它,得到URL、Height、Width;我想用java对象数组,存放Thumbnail中的值,怎么做啊?


JSONArray jsonArr = new JSONArray(jsonstr);//通过jsonstr字符串构造JSONArray对象
JSONObject obj = (JSONObject)jsonArr.get(0);//获取jsonArr里第一个对象并把它赋值给 //JSONObject对象--因为你这里第一个对象是一个json串
//获取json串里Thumbnail属性的值,因为还是一个json串,这里还转换为JSONObject对象

JSONObject thumbnail = (JSONObject)obj.get("Thumbnail");
//获取Url属性,得到值

String url = (String)thumbnail.get("Url");

//只是示范一下,这里取到的是第一个Thumbnail的Url属性的值,相信看后你应该能取到你想要的

你把这个字符串反向解析成为一个 json对象,再通过json类提供的方法读取里面的值即可。

--------

---前端组装成json格式的数据
	addHoliday : function(btn){
var allRecords = [];
Ext.each(this.PPanel.datePick,function(pick){
var records = pick.getSelectDates();
Ext.each(records,function(record){
Ext.apply(record.json,{datetype:btn.datetype,color:btn.color});
allRecords.push(record.json);
},this);
},this);
var data = Ext.util.JSON.encode(allRecords);
Ext.Ajax.request({
url : 'DateSet/DateSet.do',
params : {object:'addHoliday',records:data,token:token},
scope : this,
callback : function(o,s,resp){
if(ajaxRequestFailure(resp.statusText)){
return;
}
var respText = Ext.util.JSON.decode(resp.responseText);
if(respText.success){
for(var i=0;i<this.PPanel.datePick.length;i++){
this.PPanel.datePick[i].reloadDate();
}
}else{
Ext.Msg.alert('错误',respText.error);
}
}
});
},


-----json格式数据
[
{
"a_num": "7",
"name": "<font color=red>7</font>",
"day": "7",
"year": "2015",
"month": "2",
"spacenum": "6",
"datetype": 3,
"color": "black"
},
{
"a_num": "8",
"name": "<font color=red>8</font>",
"day": "8",
"year": "2015",
"month": "2",
"spacenum": "6",
"datetype": 3,
"color": "black"
},
{
"a_num": "14",
"name": "<font color=red>14</font>",
"day": "14",
"year": "2015",
"month": "2",
"spacenum": "6",
"datetype": 3,
"color": "black"
}
]


-----java_片段
String data = uFunc.getString(request.getParameter("records"));//uFunc就是去除空格和特殊字符
JSONArray jArray = JSONArray.fromObject(data);
JSONObject jsonObject;
String sSQL = "",year,month,day,datetype,color;
List<String> sqlList = new ArrayList<String>();
for(int i = 0; i < jArray.size(); i++){
jsonObject = jArray.getJSONObject(i);
year = uFunc.getJSONString(jsonObject, "year");
if(uFunc.IsNullString(year))
continue;
month = uFunc.getJSONString(jsonObject, "month");
day = uFunc.getJSONString(jsonObject, "day");
datetype = uFunc.getJSONString(jsonObject, "datetype");
color = uFunc.getJSONString(jsonObject, "color");
sSQL = "update sw_work.dateset a set a.datetype = '"+datetype+"',a.color='"+color+"' where a.year="+year+" and a.month="+month+" and a.day="+day;
sqlList.add(sSQL);//当批量执行需要提交事务的语句时 用sqlList来添加执行;一条语句时可直接在execute中写sql语句。
}
try{
this.getBaseDao().execute(sqlList);
uFunc.writeSuccessToResponse(response, "操作成功");
}catch(Exception ex){
ex.printStackTrace();
uFunc.writeErrorToResponse(response, ex.getMessage());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值