1. 程式人生 > >JavaBean與JSON的轉換

JavaBean與JSON的轉換

<p><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">阿里巴巴開源的FastJSON,效率是非常高的。一般用來轉大量的JSON資料。</span></p>

下面介紹一些應用:

將JSON資料轉換成JavaBean

JSON:

<pre name="code" class="plain">{ "EncodeCapability" : { "ChannelMaxSetSync" : 0, "CombEncodeInfo" : [ { "CompressionMask" : "0x00000000", "Enable" : true, "HaveAudio" : true, "ResolutionMask" : "0x00000000", "StreamType" : "MainStream" }, { "CompressionMask" : "0x00000000", "Enable" : false, "HaveAudio" : false, "ResolutionMask" : "0x00000000", "StreamType" : "ExtraStream2" }, { "CompressionMask" : "0x00000000", "Enable" : false, "HaveAudio" : false, "ResolutionMask" : "0x00000000", "StreamType" : "ExtraStream3" }, { "CompressionMask" : "0x00000000", "Enable" : false, "HaveAudio" : false, "ResolutionMask" : "0x00000000", "StreamType" : "ExtraStream4" }, { "CompressionMask" : "0x00000000", "Enable" : false, "HaveAudio" : false, "ResolutionMask" : "0x00000000", "StreamType" : "JPEGStream" } ], "Compression" : "0x00000000", "EncodeInfo" : [ { "CompressionMask" : "0x00000080", "Enable" : true, "HaveAudio" : true, "ResolutionMask" : "0xF5FFFFFF", "StreamType" : "MainStream" }, { "CompressionMask" : "0x00000080", "Enable" : true, "HaveAudio" : true, "ResolutionMask" : "0xF5F7FFFF", "StreamType" : "ExtraStream2" }, { "CompressionMask" : "0x00000000", "Enable" : false, "HaveAudio" : false, "ResolutionMask" : "0x00000000", "StreamType" : "ExtraStream3" }, { "CompressionMask" : "0x00000000", "Enable" : false, "HaveAudio" : false, "ResolutionMask" : "0x00000000", "StreamType" : "ExtraStream4" }, { "CompressionMask" : "0x00000080", "Enable" : true, "HaveAudio" : true, "ResolutionMask" : "0xF5F7FFFF", "StreamType" : "JPEGStream" } ], "ExImageSizePerChannel" : [ "0x00000009" ], "ExImageSizePerChannelEx" : [ [ "0x00000018", "0x55402CD8", "0x402E5988", "0x00000018", "0x00000010", "0x00000078", "0x00000040", "0x55402D50", "0x402E7D78", "0x00000010", "0x00000057", "0x00000009", "0x00000009", "0x00000011", "0x00000009", "0x400834B9", "0xFFFFFFFF", "0x00000000", "0x00000073", "0x00000000", "0x00000020", "0x00000000", "0x00000001", "0x00000013", "0x00010000", "0x00000000", "0x00000000", "0x00000000", "0x00000000", "0x00000000", "0x00000000", "0x00000000" ] ], "ImageSizePerChannel" : [ "0x00000801" ], "MaxBitrate" : 49152, "MaxEncodePower" : 38016000, "MaxEncodePowerPerChannel" : [ "0x02441400" ] }, "Name" : "EncodeCapability", "Ret" : 100, "SessionID" : "0x00000289" }
JavaBean:
package com.mobile.bean;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

public class EncodeCapabilityJSON {
	@JSONField(name="ChannelMaxSetSync")
	public Integer channelMaxSetSync;
	@JSONField(name="CombEncodeInfo")
	public List<CombAndEncodeInfo> combEncodeInfo = new ArrayList<CombAndEncodeInfo>();
	@JSONField(name="Compression")
	public String compression;
	@JSONField(name="EncodeInfo")
	public List<CombAndEncodeInfo> encodeInfo = new ArrayList<CombAndEncodeInfo>();
	@JSONField(name="ExImageSizePerChannel")
	public List<String> exImageSizePerChannel;
	@JSONField(name="ExImageSizePerChannelEx")
	public String exImageSizePerChannelEx[][];
	@JSONField(name="ImageSizePerChannel")
	public List<String> imageSizePerChannel;
	@JSONField(name="MaxEncodePowerPerChannel")
	public List<String> maxEncodePowerPerChannel;
	@JSONField(name="MaxBitrate")
	public Integer maxBitrate;
	@JSONField(name="MaxEncodePower")
	public Integer maxEncodePower;

	public Integer getChannelMaxSetSync() {
		return channelMaxSetSync;
	}

	public void setChannelMaxSetSync(Integer channelMaxSetSync) {
		this.channelMaxSetSync = channelMaxSetSync;
	}

	public List<CombAndEncodeInfo> getCombEncodeInfo() {
		return combEncodeInfo;
	}

	public void setCombEncodeInfo(List<CombAndEncodeInfo> combEncodeInfo) {
		this.combEncodeInfo = combEncodeInfo;
	}

	public String getCompression() {
		return compression;
	}

	public void setCompression(String compression) {
		this.compression = compression;
	}

	public List<CombAndEncodeInfo> getEncodeInfo() {
		return encodeInfo;
	}

	public void setEncodeInfo(List<CombAndEncodeInfo> encodeInfo) {
		this.encodeInfo = encodeInfo;
	}

	public List<String> getExImageSizePerChannel() {
		return exImageSizePerChannel;
	}

	public void setExImageSizePerChannel(List<String> exImageSizePerChannel) {
		this.exImageSizePerChannel = exImageSizePerChannel;
	}

	public String[][] getExImageSizePerChannelEx() {
		return exImageSizePerChannelEx;
	}

	public void setExImageSizePerChannelEx(String[][] exImageSizePerChannelEx) {
		this.exImageSizePerChannelEx = exImageSizePerChannelEx;
	}

	public List<String> getImageSizePerChannel() {
		return imageSizePerChannel;
	}

	public void setImageSizePerChannel(List<String> imageSizePerChannel) {
		this.imageSizePerChannel = imageSizePerChannel;
	}

	public List<String> getMaxEncodePowerPerChannel() {
		return maxEncodePowerPerChannel;
	}

	public void setMaxEncodePowerPerChannel(List<String> maxEncodePowerPerChannel) {
		this.maxEncodePowerPerChannel = maxEncodePowerPerChannel;
	}

	public Integer getMaxBitrate() {
		return maxBitrate;
	}

	public void setMaxBitrate(Integer maxBitrate) {
		this.maxBitrate = maxBitrate;
	}

	public Integer getMaxEncodePower() {
		return maxEncodePower;
	}

	public void setMaxEncodePower(Integer maxEncodePower) {
		this.maxEncodePower = maxEncodePower;
	}

}
轉換的方法:
<pre name="code" class="java">	/**
	* @Title: json2String 
	* @Description: TODO 
	* @param @param json
	* @param @return     
	* @return String    
	* @throws 
	* @add 提前JSON中包含的JSON,對包含的JSON處理,轉換成JavaBean
	 */
	public static String json2String(String json) {
		JSONObject jo = JSON.parseObject(json);
		Set<String> key = jo.keySet();
		Iterator iterator = key.iterator();
		String keyName = null;
		String keyClassName = null;
		while (iterator.hasNext()) {
			keyName = (String) iterator.next();
			keyClassName = jo.get(keyName).getClass().getName();
			if (("com.alibaba.fastjson.JSONObject").equals(keyClassName)) {
				return jo.getString(keyName);
			}
		}
		return null;
	}
	
	/**
	 * 
	* @Title: jsonstr2JavaBean 
	* @Description: 將json和bean物件進行轉換
	 */
	public static Object jsonstr2JavaBean(String json, Object object) {
		object = JSON.parseObject(json, object.getClass());
		return object;
	}
	
....
<pre name="code" class="java"><pre name="code" class="java">EncodeCapabilityJSON bcj = new EncodeCapabilityJSON();
						bcj = (EncodeCapabilityJSON)jsonstr2JavaBean(json2String(jsonString),bcj);




總結下,主要使用到的方法是:JSON.parseObject(String text, Class<? extends Object> clazz),可以將JSON直接轉換成Bean。

需要注意的是,我們接到的JSON資料,往往也要傳出去,傳出去的時候,首字母可能會大寫,這個時候使用註解幫忙,

@JSONField(name="ChannelMaxSetSync")
public Integer channelMaxSetSync;

它會將資料一一對應傳輸出去。

當然還有一些其他方法可能會被用到。

import com.alibaba.fastjson.JSON;

public static final Object parse(String text); // 把JSON文字parse為JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文字parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文字parse為JavaBean
public static final JSONArray parseArray(String text); // 把JSON文字parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文字parse成JavaBean集合
public static final String toJSONString(Object object); // 將JavaBean序列化為JSON文字
public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化為帶格式的JSON文字
public static final Object toJSON(Object javaObject);// 將JavaBean轉換為JSONObject或者JSONArray。

值得注意的是,FastJson單次只能解析一個JSON資料,如果JSON中包含其他JSON的話,其他JSON是不能被解析的。(我還沒找到好的方法)