2016-06-11 56 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答案一样。谢谢。 –