2015-10-30 29 views
7

null値で自分のクラスを逆シリアル化しようとしています。しかし、私のコードは動作しません。gsonとnullの値で逆シリアル化

マイJSON:私の方法では

{"Text":null,"Code":0,"Title":"This is Sparta!"} 

私は次のようにします。

this.setText(gson.fromJson(jsonObject.getString("Text"), String.class)); 
this.setTitle(gson.fromJson(jsonObject.getString("Title"), String.class)); 
this.setCode(gson.fromJson(jsonObject.getString("Faccode"), Integer.class)) 

私はあまりにも、List<T>が存在する可能性があるため、全体のオブジェクトをデシリアライズしていません。

エラー:

myapp W/System.err? com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6 path $ 
myapp W/System.err? at com.google.gson.Gson.assertFullConsumption(Gson.java:786) 
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:776) 
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:724) 
myapp W/System.err? at com.google.gson.Gson.fromJson(Gson.java:696) 
+0

あなたのエラーは何ですか? – ThomasThiebaud

+1

GSONで最初に解析することについて読む必要があります –

+0

あなたの質問からソリューションを削除し、適切な回答として提出できますか? (メタの質問http://meta.stackoverflow.com/q/309266/2564301に少し関連しています) – usr2564301

答えて

14

まず、あなたがgsonを使用して解析する方法について読む必要があります。あなたはhereの例を見つけることができます。

ここで解析する方法が分かっているので、引き続きヌル値に問題があります。それを解決するには、にgson伝える必要があります(デ)ためには、(ドキュメントに基づいてテストされていない、)serializeNulls()doc

Configure Gson to serialize null fields. By default, Gson omits all fields that are null during serialization.

EDIT

から

Gson gson = new GsonBuilder().serializeNulls().create(); 

を使用してnullをシリアライズあなたが行うことができるいくつかの別個の価値を得る。

String json = ""; //Your json has a String 
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); 

//If null, use a default value 
JsonElement nullableText = jsonObject.get("Text"); 
String text = (nullableText instanceof JsonNull) ? "" : nullableText.getAsString(); 

String title = jsonObject.get("Title").toString(); 
int code = jsonObject.get("Code").getAsInt(); 

それ以外の場合は、あなたがこのPOJO

public class MyElement { 
    @SerializedName("Text") 
    private String text; 

    @SerializedName("Title") 
    private String title; 

    @SerializedName("Code") 
    private int code; 
} 

を持っている場合は、

String json = ""; //Your json has a String 
Gson gson = new GsonBuilder().serializeNulls().create(); 
MyElement myElement = gson.fromJson(json, MyElement.class); 
+1

私は今、たくさん読んでいます。私はGson gson = new GsonBuilder()を試しました。serializeNulls()。create();も同様です。しかし、それを得ていない。どのように正しく解析する必要がありますか?そして問題はシリアル化ではなく、DEシリアル化で – BHuelse

+0

私の答えを編集しました。私はそれが助けてくれることを願うそれ以外の場合は詳細を提供できますか? – ThomasThiebaud

+0

これで動作します。私もJsonParserを使いました。しかし、私は古いコードからJsonObjectの代わりにJSONObjectを持っています。 – BHuelse

0

を使用して解析することができ、私は以下のPOJOと同様の問題(例外はnull値に投げ)持っていた:

public class MyElement { 
    private String something; 
    private String somethingElse; 
    private JsonObject subEntry; // this doesn't allow deserialization of `null`! 
} 

とこのコード:

parsedJson = gson.fromJson(json, MyElement.class) 

subEntryがバックエンドによって返されたときはnullでした。

Iはnull値のデシリアライゼーションを可能にするために、JsonObjectJsonNull両方の親クラスであるJsonElementJsonObjectからsubEntryの種類を変更することによってそれを修正。後で次のようにあなたがやるだろうと、実行時にはnullをチェックするために

public class MyElement { 
    private String something; 
    private String somethingElse; 
    private JsonElement subEntry; // this allows deserialization of `null` 
} 

if (parsedJson.subEntry instanceof JsonNull) { 
    ... 
} else { 
    ... 
} 
+0

も参照[この回答](https://stackoverflow.com/questions/12379405/cant-cast-jsonnull-to-jsonobject) –

関連する問題