来自同名json的Java类

来自同名json的Java类,java,json,gson,Java,Json,Gson,我有一个JSON对象,如下所示: ... { "url": "checkout.bodenusa.com/en-US" }, { "url": [ ".bonton.com/checkout/", ".bonton.com/CheckoutView" ] } ... 对于响应服务器,我的Java类应该是什么样子 我尝试了这个片段,但它不正确: @SerializedNam

我有一个JSON对象,如下所示:

...
    {
       "url": "checkout.bodenusa.com/en-US"
    },
    {
       "url": [
            ".bonton.com/checkout/",
            ".bonton.com/CheckoutView"
        ]
    }
...
对于响应服务器,我的Java类应该是什么样子

我尝试了这个片段,但它不正确:

@SerializedName("url")
@Expose
private List<String> urlList = null;
@SerializedName("url")
@Expose
private String url;
@SerializedName(“url”)
@暴露
私有列表urlList=null;
@序列化名称(“url”)
@暴露
私有字符串url;
使用此选项生成POJO的

创建一个类似

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Model {

    @SerializedName("url")
    @Expose
    private List<String> url = null;
    @SerializedName("apply")
    @Expose
    private Apply apply;
    @SerializedName("controls")
    @Expose
    private Controls controls;
    @SerializedName("remove")
    @Expose
    private Remove remove;

    public List<String> getUrl() {
        return url;
    }

    public void setUrl(List<String> url) {
        this.url = url;
    }

    public Apply getApply() {
        return apply;
    }

    public void setApply(Apply apply) {
        this.apply = apply;
    }

    public Controls getControls() {
        return controls;
    }

    public void setControls(Controls controls) {
        this.controls = controls;
    }

    public Remove getRemove() {
        return remove;
    }

    public void setRemove(Remove remove) {
        this.remove = remove;
    }


    public class Controls {

        @SerializedName("promo")
        @Expose
        private String promo;
        @SerializedName("total")
        @Expose
        private String total;
        @SerializedName("orderTotal")
        @Expose
        private String orderTotal;
        @SerializedName("coupon")
        @Expose
        private String coupon;

        public String getPromo() {
            return promo;
        }

        public void setPromo(String promo) {
            this.promo = promo;
        }

        public String getTotal() {
            return total;
        }

        public void setTotal(String total) {
            this.total = total;
        }

        public String getOrderTotal() {
            return orderTotal;
        }

        public void setOrderTotal(String orderTotal) {
            this.orderTotal = orderTotal;
        }

        public String getCoupon() {
            return coupon;
        }

        public void setCoupon(String coupon) {
            this.coupon = coupon;
        }

    }

    public class Remove {

        @SerializedName("type")
        @Expose
        private String type;
        @SerializedName("submit")
        @Expose
        private String submit;
        @SerializedName("timeout")
        @Expose
        private Integer timeout;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getSubmit() {
            return submit;
        }

        public void setSubmit(String submit) {
            this.submit = submit;
        }

        public Integer getTimeout() {
            return timeout;
        }

        public void setTimeout(Integer timeout) {
            this.timeout = timeout;
        }

    }

    public class Apply {

        @SerializedName("type")
        @Expose
        private String type;
        @SerializedName("submit")
        @Expose
        private String submit;
        @SerializedName("timeout")
        @Expose
        private Integer timeout;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getSubmit() {
            return submit;
        }

        public void setSubmit(String submit) {
            this.submit = submit;
        }

        public Integer getTimeout() {
            return timeout;
        }

        public void setTimeout(Integer timeout) {
            this.timeout = timeout;
        }

    }

}

这是无效的json:
{“1”},{“2”}
您的数据是无效的json变量将URL对象修改如下:
“URL”:[“1”,“2”]
@n00dl3我不只是示例,我还以为您自以为是。我更正了我的示例。这是一个糟糕的设计,我的意思是在json中从
String
切换到
String[]
听起来是个糟糕的主意。我想,你需要实现某种类型的。但是如果您有选择的话,我建议您只使用字符串[]。请仔细考虑。我得到了数组。继续使用我提供的链接尝试json。一个问题。我有日期反序列化.setDateFormat(“MM/dd/yyyy”)。现在我遇到了错误com.google.gson.JsonSyntaxException:4/10/2017,我如何在ArrayAdapter或ArrayAdapterFactory中传输反序列化到目前为止?
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Model {

    @SerializedName("url")
    @Expose
    private List<String> url = null;
    @SerializedName("apply")
    @Expose
    private Apply apply;
    @SerializedName("controls")
    @Expose
    private Controls controls;
    @SerializedName("remove")
    @Expose
    private Remove remove;

    public List<String> getUrl() {
        return url;
    }

    public void setUrl(List<String> url) {
        this.url = url;
    }

    public Apply getApply() {
        return apply;
    }

    public void setApply(Apply apply) {
        this.apply = apply;
    }

    public Controls getControls() {
        return controls;
    }

    public void setControls(Controls controls) {
        this.controls = controls;
    }

    public Remove getRemove() {
        return remove;
    }

    public void setRemove(Remove remove) {
        this.remove = remove;
    }


    public class Controls {

        @SerializedName("promo")
        @Expose
        private String promo;
        @SerializedName("total")
        @Expose
        private String total;
        @SerializedName("orderTotal")
        @Expose
        private String orderTotal;
        @SerializedName("coupon")
        @Expose
        private String coupon;

        public String getPromo() {
            return promo;
        }

        public void setPromo(String promo) {
            this.promo = promo;
        }

        public String getTotal() {
            return total;
        }

        public void setTotal(String total) {
            this.total = total;
        }

        public String getOrderTotal() {
            return orderTotal;
        }

        public void setOrderTotal(String orderTotal) {
            this.orderTotal = orderTotal;
        }

        public String getCoupon() {
            return coupon;
        }

        public void setCoupon(String coupon) {
            this.coupon = coupon;
        }

    }

    public class Remove {

        @SerializedName("type")
        @Expose
        private String type;
        @SerializedName("submit")
        @Expose
        private String submit;
        @SerializedName("timeout")
        @Expose
        private Integer timeout;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getSubmit() {
            return submit;
        }

        public void setSubmit(String submit) {
            this.submit = submit;
        }

        public Integer getTimeout() {
            return timeout;
        }

        public void setTimeout(Integer timeout) {
            this.timeout = timeout;
        }

    }

    public class Apply {

        @SerializedName("type")
        @Expose
        private String type;
        @SerializedName("submit")
        @Expose
        private String submit;
        @SerializedName("timeout")
        @Expose
        private Integer timeout;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getSubmit() {
            return submit;
        }

        public void setSubmit(String submit) {
            this.submit = submit;
        }

        public Integer getTimeout() {
            return timeout;
        }

        public void setTimeout(Integer timeout) {
            this.timeout = timeout;
        }

    }

}
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;

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

public class ArrayAdapterFactory implements TypeAdapterFactory {

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {

        TypeAdapter<T> typeAdapter = null;
        try {
            if (type.getRawType() == List.class || type.getRawType() == ArrayList.class) {

                typeAdapter = new ArrayAdapter(gson,
                        (Class) ((ParameterizedType) type.getType())
                                .getActualTypeArguments()[0]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return typeAdapter;

    }

}
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


class ArrayAdapter<T> extends TypeAdapter<List<T>> {

    private Class<T> adapterclass;
    private Gson gson;

    public ArrayAdapter(Gson gson, Class<T> adapterclass) {
        this.adapterclass = adapterclass;
        this.gson = gson;
    }

    @Override
    public List<T> read(JsonReader reader) throws IOException {

        List<T> list = new ArrayList<T>();

        final JsonToken token = reader.peek();
        System.out.println(token);
        // Handling of Scenario 2( Check JavaDoc for the class) :
        if (token == JsonToken.STRING || token == JsonToken.NUMBER ||
                token == JsonToken.BOOLEAN) {
            T inning = (T) gson.fromJson(reader, adapterclass);
            list.add(inning);
        } else if (token == JsonToken.BEGIN_OBJECT) {
            // Handling of Scenario 1(Check JavaDoc for the class) :
            T inning = (T) gson.fromJson(reader, adapterclass);
            list.add(inning);
        } else if (token == JsonToken.BEGIN_ARRAY) {
            reader.beginArray();
            while (reader.hasNext()) {
                @SuppressWarnings("unchecked")
                T inning = (T) gson.fromJson(reader, adapterclass);
                list.add(inning);
            }
            reader.endArray();
        }

        return list;
    }

    @Override
    public void write(JsonWriter writer, List<T> value) throws IOException {

    }


}
Gson gson  = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();