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是不能被解析的。(我还没找到好的方法)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
com.alibaba.fastjson.JSONException 是一个 JSON 解析/生成异常类,通常是由 fastjson 库在处理 JSON 字符串时发生错误导致的。 出现 "com.alibaba.fastjson.JSONException: write JavaBean error" 错误一般是由以下原因引起的: 1. JavaBean 对象和 JSON 字符串之间的属性不匹配。例如,JavaBean 对象缺少某些属性或 JSON 字符串包含了多余的属性,导致无法正确地将 JSON 字符串转换JavaBean 对象,或者无法将 JavaBean 对象转换成相应的 JSON 字符串。 解决方法:检查 JavaBean 对象和 JSON 字符串之间的属性是否一一对应并正确设置。 2. JavaBean 对象的属性和 JSON 字符串的属性类型不匹配。例如,JavaBean 对象中的属性是长整型,但是 JSON 字符串中对应的属性是字符串类型,导致类型转换错误。 解决方法:确保 JavaBean 对象的属性类型和 JSON 字符串中对应的属性类型一致。 3. JavaBean 对象中存在循环引用。即 JavaBean 对象的属性中包含了对自身的引用,导致 fastjson 无法正确处理。 解决方法:避免在 JavaBean 对象中出现循环引用,可以通过在 JavaBean 对象中使用 @JSONField(serialize=false) 注解来标记该属性不被序列化。 总之,"com.alibaba.fastjson.JSONException: write JavaBean error" 错误通常是由于 fastjson 在处理 JSON 字符串和 JavaBean 对象之间的转换时出现了问题。要解决这个错误,需要检查 JavaBean 对象和 JSON 字符串之间的属性是否匹配,并且确保属性类型一致。另外,要避免在 JavaBean 对象中出现循环引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值