android解析json
来源:互联网 发布:linux kvm宿主os 编辑:程序博客网 时间:2024/06/06 05:54
下面是这个工具类的代码:
package com.zhangkeinfo.json.util;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
/**
* json util class ,use in for analyzing json
* @author spring sky
* My name :石明政
* QQ : 840950105
* 2011-12-14 15:39:51
*
*/
public class JsonUtil {
private static final String TAG = "jsonUtil";
private JSONObject jsonObject;
private JsonUtil(String json)
{
Log.e(TAG, "json="+json);
jsonObject = getJsonObject(json);
if(jsonObject==null)
{
Log.e(TAG, "jsonobject is null");
}
}
public JsonUtil() {
super();
}
public static JsonUtil newJsonUtil(String json)
{
JsonUtil util = new JsonUtil(json);
return util;
}
/**
* get json object
* @param json json data
* @return JOSNObject
*/
public JSONObject getJsonObject(String json)
{
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
} catch (JSONException e) {
Log.e(TAG, "create jsonobject exception");
e.printStackTrace();
}
return jsonObject;
}
/**
* get String data
* @param json json data
* @param key param
* @return String data
* @throws JSONException
*/
public String getString(String key) throws JSONException
{
if(jsonObject!= null)
{
return jsonObject.getString(key);
}else{
return null;
}
}
/**
* get String data
* @param json json data
* @param key param
* @return int data
* @throws JSONException
*/
public int getInt(String key) throws JSONException
{
if(jsonObject!= null)
{
return jsonObject.getInt(key);
}else{
return -1;
}
}
/**
* get Double data
* @param json json data
* @param key param
* @return double data
* @throws JSONException
*/
public double getDouble(String key) throws JSONException
{
if(jsonObject!= null)
{
return jsonObject.getDouble(key);
}else{
return -1;
}
}
/**
* This Method use in jsonObject get current class with object
* @param jsonObject
* @param c class
* @return object
* @throws Exception
*/
public Object getObject(Class<?> c) throws Exception
{
if(jsonObject!=null)
{
return getObject(c.getSimpleName().toLowerCase(),c);
}else{
return null;
}
}
/**
* This Method use in jsonObject get current class with object
* @param jsonObject
* @param key query key
* @param c class
* @return object
* @throws Exception
*/
public Object getObject(String key,Class<?> c) throws Exception
{
if(jsonObject!=null)
{
return getObject(jsonObject, key, c);
}else{
return null;
}
}
public Object getObject(JSONObject jsonObject,Class<?> c) throws Exception
{
return getObject(jsonObject, c.getSimpleName().toLowerCase(), c);
}
/**
* This Method use in jsonObject get current class with object
* @param jsonObject
* @param key query key
* @param c class
* @return object
* @throws Exception
*/
public Object getObject(JSONObject jsonObject, String key,Class<?> c) throws Exception
{
Log.e(TAG,"key == " + key);
Object bean =null ;
if(jsonObject!=null)
{
JSONObject jo = null;
if(key!=null)
{
jo = jsonObject.getJSONObject(key);
}else{
jo = jsonObject;
}
if(jo!=null)
{
if(c.equals(null))
{
Log.e(TAG, "class is null");
bean = jo.get(key);
}else{
bean = c.newInstance();
Field[] fs = c.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true);
Type type = f.getGenericType();
String value = jo.getString(f.getName());
Log.e(TAG,f.getName()+"="+value);
if(type.equals(int.class))
{
f.setInt(bean,Integer.valueOf(value));
}else if(type.equals(double.class)){
f.setDouble(bean, Double.valueOf(value));
}else{
f.set(bean,value);
}
}
}
}else{
Log.e(TAG, "in jsonobject not key ");
}
}else{
Log.e(TAG, "current param jsonobject is null");
}
return bean;
}
/**
* This method use in jsonObject get list object
* @param key list key
* @param objectKey object key
* @param c object
* @return list
* @throws Exception
*/
public List<Object> getList(String key ,Class<?> c) throws Exception
{
List<Object> list = null;
if(jsonObject!=null)
{
JSONArray jsonArray = jsonObject.getJSONArray(key);
if(!jsonArray.isNull(0))
{
list = new ArrayList<Object>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsObject = jsonArray.getJSONObject(i);
Object object = getObject(jsObject, null, c);
list.add(object);
}
}
}
return list;
}
/**
* Test class field value
* @param c
* @param classObject
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static String getFieldValue(Class<?> c,Object classObject) throws IllegalArgumentException, IllegalAccessException
{
StringBuffer sb = new StringBuffer();
Field[] fs = c.getFields();
for (int i = 0; i < fs.length; i++) {
String s = fs[i].getName()+"="+fs[i].get(classObject);
sb.append(s).append("\n");
}
// Log.e(TAG, sb.toString());
return sb.toString();
}
}
一个测试的实体类:
wallpaper :
package com.zhangkeinfo.json.model.response;
public class Wallpaper{
public int id;
public String name;
public int rank;
public String img;
public String preview1url;
public String preview2url;
public String preview3url;
public String msg;
public int ptotal;
}
Price:
package com.zhangkeinfo.json.model.response;
public class Price {
public int id;
public int price ;
public String name;
}
解析测试:
package com.zhangkeinfo.json;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.zhangkeinfo.json.model.request.ZmxURL;
import com.zhangkeinfo.json.model.response.Price;
import com.zhangkeinfo.json.model.response.Wallpaper;
import com.zhangkeinfo.json.network.NetWorkUtil;
import com.zhangkeinfo.json.util.JsonUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity1 extends Activity implements OnClickListener{
private static final String TAG = "MainActivity";
Button button1,button2;
TextView text;
/**
* 测试的json数据
*/
public String json = "{\"id\":\"17\",\"res\":\"1\",\"wallpaper\":{\"id\":\"50\",\"name\":\"壁纸1\",\"rank\":\"0\",\"img\":\"img/a.png\",\"preview1url\":\"img/a.png\",\"preview2url\":\"img/a.png\",\"preview3url\":\"img/a.png\",\"msg\":\"\",\"ptotal\":\"1\"},\"price\":[{\"id\":\"1\",\"price\":\"1\",\"name\":\"点播\"},{\"id\":\"2\",\"price\":\"6\",\"name\":\"包月\"}]}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
try {
StringBuffer sb = new StringBuffer();
/**
* 得到实例对象
*/
JsonUtil util1 = JsonUtil.newJsonUtil(json);
/*
* 得到list对象
*/
List<Object> list = util1.getList("prices", Price.class);
for(int i =0 ;i< list.size();i++)
{
Price price = (Price) list.get(i);
sb.append(JsonUtil.getFieldValue(Price.class, price));
}
/**
* 得到对象
*/
Wallpaper wallpaper = (Wallpaper) util1.getObject( Wallpaper.class);
if(wallpaper!=null)
{
/**
* 反射得到字段的内容
*/
Field[] fs = Wallpaper.class.getFields();
for (int i = 0; i < fs.length; i++) {
String s = fs[i].getName()+"="+fs[i].get(wallpaper);
sb.append(s).append("\n");
}
}else{
sb.append("wallpaper is null");
}
text.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
- 【json】android json解析
- Android-Android解析JSON
- Android数据解析:Json解析
- android---Json数据解析
- Android Json 数据解析
- android 解析JSON
- android 解析json数据格式
- android 解析json数据格式
- Android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- Android的JSON解析
- Android 解析Json
- android JSON 解析
- Android 解析json数据格式
- android解析Json
- Android解析json数据
- Android Json解析
- 个人(做人做事工作)心得
- 企业运营中的渐进螺旋演化套路与线性思维
- Android项目导入出现问题解决方案
- 下载频道12月热门资源TOP100强力推荐!
- 安卓巴士总结了近百个Android优秀开源项目,覆盖Android开发的每个领域
- android解析json
- .net 平台下的webservice利用soapheader自定义信息做验证
- Arch Linux命令
- Textview如何将Html语言中的空格进行转换并实现首行缩进
- 一号店拆库的那点事
- 系统启动过程
- Struts2 标签加样式
- MYSQL中EXPLAIN的说明
- mediacontroller无法显示问题