关闭

java的对象-》jason-》String-》byte数组的转化方法。

269人阅读 评论(0) 收藏 举报
分类:

java的对象-》jason-》String-》byte数组的转化方法。

作用:可用于将对象信息转化成byte数组,然后插入到oracle的BLOB类型的数据表中。存储到数据库中。


注意点:需要引入相关的工具的jar包。

相关代码及实例类如下:

package test;


import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import mapper.GoodsFav;
import mapper.HdPayMentResponse;
import net.sf.json.JSONObject;


public class JsonHelper {


public static void main(String[] args) {
//
HdPayMentResponse hdPayMentResponse=new HdPayMentResponse();
hdPayMentResponse.setPayNo("qwe");
hdPayMentResponse.setAmount(new BigDecimal(123));

List<GoodsFav>  goodsfavList=new ArrayList<GoodsFav>();
GoodsFav goodsFav1=new GoodsFav();
GoodsFav goodsFav2=new GoodsFav();
GoodsFav goodsFav3=new GoodsFav();

goodsFav1.setAmount(1.1);
goodsFav1.setLineNumber(1);

goodsFav2.setAmount(1.2);
goodsFav2.setLineNumber(2);

goodsFav3.setAmount(1.3);
goodsFav3.setLineNumber(3);

goodsfavList.add(goodsFav1);
goodsfavList.add(goodsFav2);
goodsfavList.add(goodsFav3);

hdPayMentResponse.setGoodsfavList(goodsfavList);

//对象转化成jason
JSONObject jSONObject=null;
jSONObject=JSONObject.fromObject(hdPayMentResponse);
System.out.println(jSONObject);

//jason转化成String
String jasonString = null;
jasonString = jSONObject.toString();
System.out.println(jasonString);

//String转化成byte数组
byte[] bytes=null;
try {
bytes=jasonString.getBytes("UTF8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

for(int i=0;i<bytes.length;i++){
System.out.println(bytes[i]);
}


}

}


package mapper;


public class GoodsFav {
// 商品行号
private Integer lineNumber;
// 分账金额
private double amount;

public Integer getLineNumber() {
return lineNumber;
}
public void setLineNumber(Integer lineNumber) {
this.lineNumber = lineNumber;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}



}




package mapper;


import java.math.BigDecimal;
import java.util.List;


public class HdPayMentResponse  {


// 支付系统流水号
private String payNo;
// 下单金额
private BigDecimal amount;
// 分摊商品优惠明细
private List<GoodsFav>  goodsfavList;

public String getPayNo() {
return payNo;
}
public void setPayNo(String payNo) {
this.payNo = payNo;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public List<GoodsFav> getGoodsfavList() {
return goodsfavList;
}
public void setGoodsfavList(List<GoodsFav> goodsfavList) {
this.goodsfavList = goodsfavList;
}


}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:593351次
    • 积分:8265
    • 等级:
    • 排名:第2519名
    • 原创:217篇
    • 转载:297篇
    • 译文:22篇
    • 评论:65条
    最新评论