Android解析——json解析为javabean

json的解析,归根结底就是用JsonObject以及JsonArray来实现的。

在日常中,当我们拿到一个较为复杂的json字符串的时候,用以上两个虽然能实现解析,但显得很麻烦。这里介绍下用Gson类来将json解析为javabean。

用两个json字符串来分析:

一、简单json字符串的解析

String JSON_SIMPLE_STRING="{\"sitename\":\"JSON\",\"siteurl\":\"www.kjson.com\",\"keyword\":\"JSON在线校验,格式化JSON,json 在线校验\",\"description\":\"JSON解析\"}";

解析方式:

<span style="white-space:pre">		</span>Gson gson=new Gson();
		UseBean useBean=gson.fromJson(JSON_SIMPLE_STRING, UseBean.class);
		System.out.println(useBean.getSitename()+" "+useBean.getSiteurl()+" "+useBean.getKeyword()+" "+useBean.getDescription());
	

对应的UseBean如下:

package com.bill99;

public class UseBean {

	private String sitename;
	private String siteurl;
	private String keyword;
	private String description;
	/**
	 * @return the sitename
	 */
	public String getSitename() {
		return sitename;
	}
	/**
	 * @param sitename the sitename to set
	 */
	public void setSitename(String sitename) {
		this.sitename = sitename;
	}
	/**
	 * @return the siteurl
	 */
	public String getSiteurl() {
		return siteurl;
	}
	/**
	 * @param siteurl the siteurl to set
	 */
	public void setSiteurl(String siteurl) {
		this.siteurl = siteurl;
	}
	/**
	 * @return the keyword
	 */
	public String getKeyword() {
		return keyword;
	}
	/**
	 * @param keyword the keyword to set
	 */
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
}


以上是简单的一个json字符串,没有嵌套,全部都是字符串。


二、嵌套json字符串的解析

String JSON_STRING="{\"array\": [ {\"h\":1, \"i\":2,\"j\":3},{\"h\":4, \"i\":5,\"j\":6} ],  \"bool\": \"true\",   \"number\": 123,  \"object\": {\"a\": \"b\",\"c\": \"d\", \"e\": \"f\"  }, \"string\": \"Hello World\"}";

这个字符串主要包含了数组以及其他的嵌套。我们用同样的方式可以得到需要的javabean,只是javabean定义较为复杂,代码如下:

<span style="white-space:pre">	</span>    MultipleBean multipleBean=gson.fromJson(JSON_STRING, MultipleBean.class);
	    System.out.println(multipleBean.getArray().size());
MultipleBean的定义如下:

package com.bill99;

import java.util.List;

public class MultipleBean {
   private List<ArrayBean> array;
   private String bool;
   private int number;
   private object object;
   private String string;
/**
 * @return the array
 */
public List<ArrayBean> getArray() {
	return array;
}
/**
 * @param array the array to set
 */
public void setArray(List<ArrayBean> array) {
	this.array = array;
}
/**
 * @return the number
 */
public int getNumber() {
	return number;
}
/**
 * @return the bool
 */
public String getBool() {
	return bool;
}
/**
 * @param bool the bool to set
 */
public void setBool(String bool) {
	this.bool = bool;
}
/**
 * @param number the number to set
 */
public void setNumber(int number) {
	this.number = number;
}
/**
 * @return the string
 */
public String getString() {
	return string;
}
/**
 * @return the object
 */
public object getObject() {
	return object;
}
/**
 * @param object the object to set
 */
public void setObject(object object) {
	this.object = object;
}
/**
 * @param string the string to set
 */
public void setString(String string) {
	this.string = string;
}
   
   
}

因为MultipleBean中包含其他的类定义,各分别定义bean如下:

package com.bill99;

public class ArrayBean {

	private int h;
	private int i;
	private int j;
	/**
	 * @return the h
	 */
	public int getH() {
		return h;
	}
	/**
	 * @param h the h to set
	 */
	public void setH(int h) {
		this.h = h;
	}
	/**
	 * @return the i
	 */
	public int getI() {
		return i;
	}
	/**
	 * @param i the i to set
	 */
	public void setI(int i) {
		this.i = i;
	}
	/**
	 * @return the j
	 */
	public int getJ() {
		return j;
	}
	/**
	 * @param j the j to set
	 */
	public void setJ(int j) {
		this.j = j;
	}
	
}

package com.bill99;

public class object {

	private String a;
	private String c;
	private String e;
	/**
	 * @return the a
	 */
	public String getA() {
		return a;
	}
	/**
	 * @param a the a to set
	 */
	public void setA(String a) {
		this.a = a;
	}
	/**
	 * @return the c
	 */
	public String getC() {
		return c;
	}
	/**
	 * @param c the c to set
	 */
	public void setC(String c) {
		this.c = c;
	}
	/**
	 * @return the e
	 */
	public String getE() {
		return e;
	}
	/**
	 * @param e the e to set
	 */
	public void setE(String e) {
		this.e = e;
	}
	
	
}

三、抽象化

通过以上的两种形式的json字符串解析为javabean。可以看出,绝大部分的json字符串都可以用这样的形式解析为我们需要的javabean,便于代码的后续操作。

在这里可以将以上使用形式抽象化:

/***
     * 将将对象转换为传入类型的对象
     * @param 
     * @param object
     * @param beanClass
     * @return
     */
    public static <T> T toBean(String json, Class<T> beanClass)
    {
        Gson gson=new Gson();
       return  (T)gson.fromJson(json, beanClass);
    }

使用方式:

 <span style="white-space:pre">	</span>    MultipleBean multipleBean2=toBean(JSON_STRING, MultipleBean.class);
	    System.out.println(multipleBean2.getArray().size());


四、另一种常用的转换HashMap

在json的转换中,有另一种较为常用的转换是:json字符串转为hashmap形式,做法如下:

/***
     * 将对象转换为HashMap
     * @param object
     * @return
     */
    public static HashMap toHashMap(Object object)
    {
        HashMap<String, Object> data = new HashMap<String, Object>();
        JSONObject jsonObject = JSONUtils.toJSONObject(object);
        Iterator it = jsonObject.keys();
        while (it.hasNext())
        {
            String key = String.valueOf(it.next());
            Object value = jsonObject.get(key);
            data.put(key, value);
        }

        return data;
    }

五、源码

http://u.download.csdn.net/upload


六、备注

如果觉得写javabean麻烦,有一个简便的方式:用工具自动生成。网址:http://www.bejson.com/json2javapojo/

猜你喜欢

转载自blog.csdn.net/yangzhaomuma/article/details/50995094