Android使用GSON无法将对象转换为JSON格式 [英] Android converting an object to JSON format with GSON not working

查看:179
本文介绍了Android使用GSON无法将对象转换为JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用GSON将对象转换为JSON格式。然后,我将把JSON发送到用PHP编写的web服务。不幸的是,由于某种原因,gson.toJson(object)方法不起作用。

我有这个java对象 - 干涉(包含字符串,日期,时间和位图类型的几个字段)。当我尝试这样做时:

  Gson gson = new Gson(); 
String json = gson.toJson(intervention);

我在gson变量中得到这个:



< blockquote>

gson = {serializeNulls:falsefactories:[Factory [type = com.google.gson.JsonElement,adapter = com.google.gson.internal.bind.TypeAdapters $ 25 @ 40fbe5c8],
com.google.gson.internal.bind.ObjectTypeAdapter$1@411b7db8,
Factory [type = java.lang.String,adapter = com.google.gson.internal.bind.TypeAdapters $ 13 @ 40ff42d0],
Factory [type = java.lang.Integer + int,adapter = com.google.gson.internal.bind.TypeAdapters $ 7 @ 411c00f8],
Factory [type = java.lang.Boolean + boolean,adapter = com.google.gson.internal.bind.TypeAdapters$3@411b4798],
Factory [type = java.lang.Byte + byte,adapter = com.google.gson.internal.bind.TypeAdapters $ 5 @ 4122dbd0],
工厂[type = java.lang.Short + short,adapter = com.google.gson.internal.bind.TypeAdapters $ 6 @ 4122a0c8],Factory [type = java.lang.Long + long,adapter = com。 google.gson.internal.bind.TypeAdapters$8@40fc5d98],
Factory [type = java.lang.Double + double,adapter = com.goog le.gson.Gson$4@412295f0],
Factory [type = java.lang.Float + float,adapter = com.google.gson.Gson $ 5 @ 41229898],
Factory [type = java。 lang.Number,adapter = com.google.gson.internal.bind.TypeAdapters $ 11 @ 41223618],
Factory [type = java.lang.Character + char,adapter = com.google.gson.internal.bind。 TypeAdapters $ 12 @ 41224168],Factory [type = java.lang.StringBuilder,adapter = com.google.gson.internal.bind.TypeAdapters $ 16 @ 411a7550],
Factory [type = java.lang.StringBuffer,adapter = com.google.gson.internal.bind.TypeAdapters$17@411a7790],
Factory [type = java.math.BigDecimal,adapter = com.google.gson.internal.bind.TypeAdapters $ 14 @ 40ff4568],
Factory [type = java.math.BigInteger,adapter = com.google.gson.internal.bind.TypeAdapters $ 15 @ 411b4440],
Factory [type = java.net.URL,adapter = com.google。 gson.internal.bind.TypeAdapters $ 18@411be338],
Factory [type = java.net.URI,adapter = com.google.gson.internal.bind.TypeAdapters $ 19 @ 41229028],
Factory [类型= java.util.UUID中,适配器= com.google.gson.i nternal.bind.TypeAdapters $ 21@411a2200],
Factory [type = java.util.Locale,adapter = com.google.gson.internal.bind.TypeAdapters $ 24 @ 411a2510],
Factory [typeHierarchy = java.net.InetAddress,adapter = com.google.gson.internal.bind.TypeAdapters $ 20 @ 41229328],
Factory [type = java.util.BitSet,adapter = com.google.gson.internal.bind。 TypeAdapters $ 2 @ 411ca140],
com.google.gson.internal.bind.DateTypeAdapter$1@412024f8,
Factory [type = java.util.Calendar + java.util.GregorianCalendar,adapter = com.google .gson.internal.bind.TypeAdapters $ 23 @ 411bef70],
com.google.gson.internal.bind.TimeTypeAdapter$1@41229920,
com.google.gson.internal.bind.SqlDateTypeAdapter$1@411b67c0 ,
com.google.gson.internal.bind.TypeAdapters$22@411a2500,
com.google.gson.internal.bind.ArrayTypeAdapter$1@41222e90,
com.google.gson.internal .bind.TypeAdapters $ 26 @ 411b7ca0,
Factory [type = java.lang.Class,adapter = com.google.gson.internal.bind.TypeAdapters $ 1 @ 4121d940],
com.google.gs on.internal.Excluder@4122a3c8,
com.google.gson.internal.bind.CollectionTypeAdapterFactory@40ff26d0,
com.google.gson.internal.bind.MapTypeAdapterFactory@40ff2980,
com。 google.gson.internal.bind.ReflectiveTypeAdapterFactory@40ff2ba0],instanceCreators:{}}




 导入android.graphics.Bitmap; 
import com.google.gson.annotations.SerializedName;
import java.util.Date;

公开课介入{

  @SerializedName(case_id)
private String case_id;
public String getCase_id(){
return case_id;
}

@SerializedName(user_id)
private String user_id;
public String getUser_id(){
return user_id;
}

@SerializedName(account_id)
private String account_id;
public String getAccount_id(){
return account_id;
}

@SerializedName(description)
私有字符串描述;
public String getDescription(){
return description;
}

@SerializedName(solution)
私有字符串解决方案;
public String getSolution(){
return solution;
}

@SerializedName(status)
private字符串状态;
public String getStatus(){
return status;
}

@SerializedName(user_remarks)
private String user_remarks;
public String getUser_remarks(){
return user_remarks;
}


@SerializedName(customer_remarks)
private String customer_remarks;
public String getCustomer_remarks(){
return customer_remarks;
}

@SerializedName(case_complete)
private boolean case_complete;
public boolean isCase_complete(){
return case_complete;
}

@SerializedName(charge_transport)
private boolean charge_transport;
public boolean isCharge_transport(){
return charge_transport;
}

@SerializedName(service_billable)
private boolean service_billable;
public boolean isService_billable(){
return service_billable;
}

@SerializedName(date_created)
私人日期date_created;
public Date getDate_created(){
return date_created;
}

@SerializedName(arrival_time)
private String arrival_time;
public String getArrival_time(){
return arrival_time;
}

@SerializedName(departure_time)
private String departure_time;
public String getDeparture_time(){
return departure_time;
}

@SerializedName(user_sign)
私人位图user_sign;
public Bitmap getUser_sign(){
return user_sign;
}

@SerializedName(cust_sign)
私人位图cust_sign;
public Bitmap getCust_sign(){
return cust_sign;
}

public void setUser_id(String user_id){
this.user_id = user_id;
}

public void setCase_id(String case_id){
this.case_id = case_id;
}


public void setAccount_id(String account_id){
this.account_id = account_id;
}



public void setDescription(String description){
this.description = description;
}



public void setSolution(String solution){
this.solution = solution;
}



public void setStatus(String status){
this.status = status;
}


public void setUser_remarks(String user_remarks){
this.user_remarks = user_remarks;
}



public void setCustomer_remarks(String customer_remarks){
this.customer_remarks = customer_remarks;
}



public void setCase_complete(boolean case_complete){
this.case_complete = case_complete;
}



public void setCharge_transport(boolean charge_transport){
this.charge_transport = charge_transport;
}

public void setService_billable(boolean service_billable){
this.service_billable = service_billable;
}


public void setDate_created(Date date_created){
this.date_created = date_created;
}

public void setArrival_time(String arrival_time){
this.arrival_time = arrival_time;
}


public void setDeparture_time(String departure_time){
this.departure_time = departure_time;
}

public void setUser_sign(位图user_sign){
this.user_sign = user_sign;
}

public void setCust_sign(Bitmap cust_sign){
this.cust_sign = cust_sign;
}

}

上面的类是对象类。

我验证了对象干预并填充了字段。有人可以向我解释我在这里做错了什么?

解决方案

当前版本的 Gson (2.2.2)对于 Date 对象没有问题,并且会很好地序列化/反序列化它们。如果您正在寻找特定的格式,您可以在 GsonBuilder 中使用 setDateFormat()来指定它。



您的位图对象是另一个问题。这是一个非常复杂的对象,而不是Gson将要知道该怎么做的事情(不要紧,甚至很难用JSON表示)。你需要编写一个自定义串行器和解串器来处理它。

观察 Bitmap 的文档,看起来您可以将像素数据提取到 int [] 数组使用 getPixels()以及从该数据构造位图使用适当的静态 createBitmap()方法。您需要编写一个序列化程序,从中产生JSON(还包括其他参数),然后使用一个反序列化程序,它将知道如何读取该JSON并返回 Bitmap

有关编写自定义序列化器/反序列化器的信息可以在Gson用户指南中找到: https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-反序列化


I am trying to convert an object to a JSON format using GSON. I will then send the JSON to the webservice written in php. Unfortunately for some reason, the method gson.toJson(object) is not working.

I have this java object - intervention (This contains several fields of types String, Date, Time and bitmaps). When I try to do this:

Gson gson = new Gson();
String json = gson.toJson(intervention);

I get this in the gson variable:

gson={serializeNulls:falsefactories:[Factory[type=com.google.gson.JsonElement,adapter=com.google.gson.internal.bind.TypeAdapters$25@40fbe5c8], com.google.gson.internal.bind.ObjectTypeAdapter$1@411b7db8, Factory[type=java.lang.String,adapter=com.google.gson.internal.bind.TypeAdapters$13@40ff42d0], Factory[type=java.lang.Integer+int,adapter=com.google.gson.internal.bind.TypeAdapters$7@411c00f8], Factory[type=java.lang.Boolean+boolean,adapter=com.google.gson.internal.bind.TypeAdapters$3@411b4798], Factory[type=java.lang.Byte+byte,adapter=com.google.gson.internal.bind.TypeAdapters$5@4122dbd0], Factory[type=java.lang.Short+short,adapter=com.google.gson.internal.bind.TypeAdapters$6@4122a0c8], Factory[type=java.lang.Long+long,adapter=com.google.gson.internal.bind.TypeAdapters$8@40fc5d98], Factory[type=java.lang.Double+double,adapter=com.google.gson.Gson$4@412295f0], Factory[type=java.lang.Float+float,adapter=com.google.gson.Gson$5@41229898], Factory[type=java.lang.Number,adapter=com.google.gson.internal.bind.TypeAdapters$11@41223618], Factory[type=java.lang.Character+char,adapter=com.google.gson.internal.bind.TypeAdapters$12@41224168], Factory[type=java.lang.StringBuilder,adapter=com.google.gson.internal.bind.TypeAdapters$16@411a7550], Factory[type=java.lang.StringBuffer,adapter=com.google.gson.internal.bind.TypeAdapters$17@411a7790], Factory[type=java.math.BigDecimal,adapter=com.google.gson.internal.bind.TypeAdapters$14@40ff4568], Factory[type=java.math.BigInteger,adapter=com.google.gson.internal.bind.TypeAdapters$15@411b4440], Factory[type=java.net.URL,adapter=com.google.gson.internal.bind.TypeAdapters$18@411be338], Factory[type=java.net.URI,adapter=com.google.gson.internal.bind.TypeAdapters$19@41229028], Factory[type=java.util.UUID,adapter=com.google.gson.internal.bind.TypeAdapters$21@411a2200], Factory[type=java.util.Locale,adapter=com.google.gson.internal.bind.TypeAdapters$24@411a2510], Factory[typeHierarchy=java.net.InetAddress,adapter=com.google.gson.internal.bind.TypeAdapters$20@41229328], Factory[type=java.util.BitSet,adapter=com.google.gson.internal.bind.TypeAdapters$2@411ca140], com.google.gson.internal.bind.DateTypeAdapter$1@412024f8, Factory[type=java.util.Calendar+java.util.GregorianCalendar,adapter=com.google.gson.internal.bind.TypeAdapters$23@411bef70], com.google.gson.internal.bind.TimeTypeAdapter$1@41229920, com.google.gson.internal.bind.SqlDateTypeAdapter$1@411b67c0, com.google.gson.internal.bind.TypeAdapters$22@411a2500, com.google.gson.internal.bind.ArrayTypeAdapter$1@41222e90, com.google.gson.internal.bind.TypeAdapters$26@411b7ca0, Factory[type=java.lang.Class,adapter=com.google.gson.internal.bind.TypeAdapters$1@4121d940], com.google.gson.internal.Excluder@4122a3c8, com.google.gson.internal.bind.CollectionTypeAdapterFactory@40ff26d0, com.google.gson.internal.bind.MapTypeAdapterFactory@40ff2980, com.google.gson.internal.bind.ReflectiveTypeAdapterFactory@40ff2ba0],instanceCreators:{}}

import android.graphics.Bitmap;
import com.google.gson.annotations.SerializedName;
import java.util.Date;

public class Intervention{

@SerializedName("case_id")
private String case_id;
public String getCase_id() {
    return case_id;
}

@SerializedName("user_id")
private String user_id;
public String getUser_id() {
    return user_id;
}

@SerializedName("account_id")
private String account_id;
public String getAccount_id() {
    return account_id;
}

@SerializedName("description")
private String description;
public String getDescription() {
    return description;
}

@SerializedName("solution")
private String solution;
public String getSolution() {
    return solution;
}

@SerializedName("status")
private String status;
public String getStatus() {
    return status;
}

@SerializedName("user_remarks")
private String user_remarks;
public String getUser_remarks() {
    return user_remarks;
}


@SerializedName("customer_remarks")
private String customer_remarks;
public String getCustomer_remarks() {
    return customer_remarks;
}

@SerializedName("case_complete")
private boolean case_complete;
public boolean isCase_complete() {
    return case_complete;
}

@SerializedName("charge_transport")
private boolean charge_transport;
public boolean isCharge_transport() {
    return charge_transport;
}

@SerializedName("service_billable")
private boolean service_billable;
public boolean isService_billable() {
    return service_billable;
}

@SerializedName("date_created")
private Date date_created;
public Date getDate_created() {
    return date_created;
}

@SerializedName("arrival_time")
private String arrival_time;
public String getArrival_time() {
    return arrival_time;
}

@SerializedName("departure_time")
private String departure_time;
public String getDeparture_time() {
    return departure_time;
}

@SerializedName("user_sign")
private Bitmap user_sign;
public Bitmap getUser_sign() {
    return user_sign;
}

@SerializedName("cust_sign")
private Bitmap cust_sign;
public Bitmap getCust_sign() {
    return cust_sign;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public void setCase_id(String case_id) {
    this.case_id = case_id;
}


public void setAccount_id(String account_id) {
    this.account_id = account_id;
}



public void setDescription(String description) {
    this.description = description;
}



public void setSolution(String solution) {
    this.solution = solution;
}



public void setStatus(String status) {
    this.status = status;
}


public void setUser_remarks(String user_remarks) {
    this.user_remarks = user_remarks;
}



public void setCustomer_remarks(String customer_remarks) {
    this.customer_remarks = customer_remarks;
}



public void setCase_complete(boolean case_complete) {
    this.case_complete = case_complete;
}



public void setCharge_transport(boolean charge_transport) {
    this.charge_transport = charge_transport;
}

public void setService_billable(boolean service_billable) {
    this.service_billable = service_billable;
}


public void setDate_created(Date date_created) {
    this.date_created = date_created;
}

public void setArrival_time(String arrival_time) {
    this.arrival_time = arrival_time;
}


public void setDeparture_time(String departure_time) {
    this.departure_time = departure_time;
}

public void setUser_sign(Bitmap user_sign) {
    this.user_sign = user_sign;
}

public void setCust_sign(Bitmap cust_sign) {
    this.cust_sign = cust_sign;
}

}

The above class is the object class.

I verified the object intervention and the fields are populated. Can someone explain to me what am I doing wrong here?

解决方案

The current version of Gson (2.2.2) has no problem with Date objects and will serialize/deserialize them just fine. If you are looking for a specific format you can use setDateFormat() in GsonBuilder to specify it.

Your Bitmap object is a different problem. That is a very complex object and not something that Gson is going to know what to do with (never mind very, very difficult to even represent in JSON). You would need to write a custom serializer and deserializer to handle it.

Glancing at the documentation for Bitmap it appears you can extract the pixel data into an int[] array using getPixels() as well as construct a Bitmap from that data using the appropriate static createBitmap() method. You would need to write a serializer that produces JSON from this (and also includes the other parameters) and then a deserializer that would know how to read that JSON and return a Bitmap.

Information on writing custom serializers/deserializers can be found in the Gson User's Guide: https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

这篇关于Android使用GSON无法将对象转换为JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆