Json学习中。。。
参考网站:
http://lyking2001.iteye.com/blog/504020
http://www.blogjava.net/madshime/archive/2009/05/11/269973.html
http://json-lib.sourceforge.net/usage.html
依赖包
- json-lib-2.2.1.jar
- jakarta commons-lang 2.4
- jakarta commons-beanutils 1.7.0
- jakarta commons-collections 3.2
- jakarta commons-logging 1.0.4
- ezmorph 1.0.6
1. 实例
//json串 变量名latlon
{
"os_type": "Google Ion;4;1.6",
"gps_location": [
{
"gps_lon": "116440437",
"gps_lat": "39959830",
"gps_accuracy": "10",
"gps_time": "51595352"
},
{
"gps_lon": "116440437",
"gps_lat": "39959830",
"gps_accuracy": "10",
"gps_time": "51595352"
}
],
"network_location": [
{
"network_lon": "116440437",
"network_lat": "39959830",
"network_accuracy": "500",
"network_time": "51595352"
},
{
"network_lon": "116440437",
"network_lat": "39959830",
"network_accuracy": "500",
"network_time": "51595352"
}
],
"cell_tower_connected_info": {
"imei": "354059021137664",
"imsi": "460022105507870",
"radio_type": "gsm",
"carrier": "中国移动",
"cell_id": "2512",
"location_area_code": "415",
"home_mobile_conutry_code": "310",
"home_mobile_network_code": "410",
"signal_strength": "-70",
"time": "51595352"
},
"wifi_tower_connected_info": {
"mac_address": "00:23:76:08:21:e7",
"wifi_ssid": "xiaonei-3G",
"wifi_bssid": "00:1f:a3:65:7f:00",
"wifi_ip_address": "192.168.1.1",
"time": "51595352"
}
}
java解析json串
JSONObject json = JSONObject.fromObject(latlon);//得到json对象
if (json != null) {
if (json.get("gps_location") instanceof JSONArray) {
JSONArray jarr = (JSONArray) json.get("gps_location");//数组操作
if (jarr != null) {
int jarrSize = jarr.size();
for (int i = 0; i < jarrSize; i++) {
JSONObject jsonLatLon = jarr.getJSONObject(i);
if (jsonLatLon != null) {
LocationData loactionData = new LocationData();
loactionData.setLatitude(jsonLatLon.optString("gps_lat"));//取值操作 用optString
loactionData.setLongitude(jsonLatLon.optString("gps_lon"));
loactionData.setAccuracy(jsonLatLon.optString("gps_accuracy"));
loactionData.setLocatType("1");
}
}
}
}
//用cellid进行基站定位
JSONObject cellObj = (JSONObject) json.getJSONObject("cell_tower_connected_info");//一般json对象操作
if (cellObj != null) {
String cellid = (String) cellObj.getString("cell_id");
String imei = (String) cellObj.getString("imei");
String imsi = (String) cellObj.getString("imsi");
String lac = (String) cellObj.getString("location_area_code");
String mnc = (String) cellObj.getString("home_mobile_network_code");
String mcc = (String) cellObj.getString("home_mobile_conutry_code");
}
}
2. java组装json串
//响应内容类型
public static final String CONTENT_TYPE_TEXT = "text/html;charset=utf-8";
public static final String CONTENT_TYPE_PLAIN = "text/plain;charset=utf-8";
//java组装json串
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
for (int i = 0; i < size; i++) {
JSONObject job = JSONObject.fromObject(list.get(i), jsonConfig);
ja.add(job);
}
jo.put("pid", pid);
jo.put("poiName", poiName);
jo.put("poiAddress", poiAddress);
jo.put("mapUrl", mapUrl);
jo.put("total", total);
jo.put("canCheckin", canCheckin);
jo.put("isTooFarAway", isTooFarAway);
//输出响应
output(response, jo.toString().getBytes(),CONTENT_TYPE_TEXT, isCompress);
//==============================================
public static void output(HttpServletResponse response, byte[] byteArray, String contentType,
boolean isCompress) {
try {
if (isCompress) {
BufferedOutputStream buffout = new BufferedOutputStream(response.getOutputStream());
GZIPOutputStream gos = new GZIPOutputStream(buffout);
gos.write(byteArray);
gos.finish();
gos.flush();
gos.close();
} else {
BufferedOutputStream buffout = new BufferedOutputStream(response.getOutputStream());
response.setContentType(contentType);
buffout.write(byteArray);
buffout.flush();
buffout.close();
}
} catch (Exception e) {
logger.error("output(HttpServletResponse, String)", e); //$NON-NLS-1$
}
}
DateJsonValueProcessor类
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* java2json日期转换器
*/
public class DateJsonValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd";
public DateJsonValueProcessor() {
}
public DateJsonValueProcessor(String format) {
this.format = format;
}
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
String[] obj = {};
if (value instanceof Date[]) {
SimpleDateFormat sf = new SimpleDateFormat(format);
Date[] dates = (Date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++) {
obj[i] = sf.format(dates[i]);
}
}
return obj;
}
@Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if (value instanceof Date) {
String str = new SimpleDateFormat(format).format((Date) value);
return str;
}
return value.toString();
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
}