Gson-使用数组或字符串的字段解析json [英] Gson - parsing json with field that is array or string

查看:127
本文介绍了Gson-使用数组或字符串的字段解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Gson解析一些json,但是我遇到了以下问题:

I'm trying to parse some json with Gson and I have following problem:

这是我的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用作字符串数组?

How to make Gson to parse it and use value as Array of String always?

推荐答案

我的建议如下:-

第1步:请对您的Decode类进行以下小的更改.

Step 1: Please make following little changes to your Decode class.

Decode.java

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

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了:

Step 3: Now its time to deserialize your json as follows:

GsonMain.java

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));
  }

 }

这篇关于Gson-使用数组或字符串的字段解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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