2016-06-11 58 views
1

我試圖解析一些JSON與GSON和我有以下問題:GSON - 解析JSON與字段是數組或字符串

這是我的JSON:

[{ 
    "label": "Check Digit", 
    "value": "W" 
}, 
{ 
    "label": "Equipment", 
    "value": [ 
    "With Standard Liftgate", 
    "(-) Version Packages" 
    ] 
}] 

這是我的Java類:

public class Decode { 

    private String label; 
    private List<String> value = new ArrayList<>(); 

    public String getLabel() { 
     return label; 
    } 

    public void setLabel(String label) { 
     this.label = label; 
    } 

    public List<String> getValue() { 
     return value; 
    } 

    public void setValue(List<String> value) { 
     this.value = value; 
    } 

} 

如何使GSON分析它,並使用value作爲字符串總是數組?

+0

我解析此JSON字符串標準: '新的Gson()。fromJson(jsonString,WholeObjectClass.class)' 我覺得它的奇怪的情況,而同一個字段可以有不同的類型。 –

+2

分享您的映射對象類代碼。 – everton

+0

我添加了java類代碼。問題在於,json中的第一個對象具有字符串而不是字符串數組。 Json由外部API提供。 –

回答

4

我的建議是如下所示: -

第1步:請對您的進行如下更改類。

Decode.java

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

public class Decode { 

private String label; 
private List<String> values = new ArrayList<>(); // in json it is "value" 

public String getLabel() { 
    return label; 
} 

public void setLabel(String label) { 
    this.label = label; 
} 

public List<String> getValues() { 
    return values; 
} 

public void setValues(List<String> values) { 
    this.values = values; 
} 

@Override 
public String toString() { 
    return "Decode [label=" + label + ", values=" + values + "]"; 
} 

} 

第2步:請創建以下JsonDeserializer

MyDeserializer.java

import java.lang.reflect.Type; 
import java.util.ArrayList; 
import java.util.List; 

import com.google.gson.Gson; 
import com.google.gson.JsonDeserializationContext; 
import com.google.gson.JsonDeserializer; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 
import com.google.gson.JsonParseException; 
import com.google.gson.reflect.TypeToken; 

public class MyDeserializer implements JsonDeserializer<Decode> { 

@Override 
public Decode deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { 
    JsonObject decodeObj = arg0.getAsJsonObject(); 
    Gson gson = new Gson(); 
    Decode decode = gson.fromJson(arg0, Decode.class); 
    List<String> values = null; 
    if (decodeObj.get("value").isJsonArray()) { 
     values = gson.fromJson(decodeObj.get("value"), new TypeToken<List<String>>() { 
     }.getType()); 
    } else { 
     String single = gson.fromJson(decodeObj.get("value"), String.class); 
     values = new ArrayList<String>(); 
     values.add(single); 
    } 
    decode.setValues(values); 
    return decode; 
} 

} 

第3步:現在就deserialize您的JSON如下:

GsonMain.java

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.util.Arrays; 

import com.google.gson.GsonBuilder; 

public class GsonMain{ 
public static void main(String[] args) throws IOException { 
    String filename = "d:/test.json"; // contains the json 
    String content = new String(Files.readAllBytes(new File(filename).toPath())); 

    GsonBuilder b = new GsonBuilder(); 
    b.registerTypeAdapter(Decode.class, new MyDeserializer()); 
    Decode[] array = b.create().fromJson(content, Decode[].class); 
    System.out.println(Arrays.toString(array)); 
    } 

} 
+0

謝謝!有用! –

0

的JSON是格式不正確,應該是:

[{ 
    "label": "Check Digit", 
    "value": ["W"] 
}, 
{ 
    "label": "Equipment", 
    "value": [ 
    "With Standard Liftgate", 
    "(-) Version Packages" 
    ] 
}] 

再次解析器類應該是:

public class Decode { 

    private String label; 
    private String[] value; 

    public String getLabel() { 
     return label; 
    } 

    public void setLabel(String label) { 
     this.label = label; 
    } 

    public String[] getValue() { 
     return value; 
    } 

    public void setValue(String[] value) { 
     this.value = value; 
    } 

} 

希望這將有助於:)

+0

感謝您的回覆。我知道這種格式很奇怪,但我從外部API接收它。我正在尋找一些方法來映射這個json。 –

+1

爲此,您可以嘗試使用您自己的反序列化器,請參閱以下鏈接:http://stackoverflow.com/questions/28319473/gson-same-field-name-different-types 讓我知道你是否仍然需要我的幫助 – Neo

+0

I使用自己的解串器就像Sanjeev Saha答案一樣。謝謝。 –