数组Json使用Gson或Xtream </myobject>列出<myobject>

时间:2012-07-11 12:14:17

标签: java arrays list gson

你好,我有这个跟随json代码。

[{"check":{"domain":"qwe.coedu.br"}},{"check":{"domain":"qwe.com.br"}},{"check":{"domain":"qwe.com"}}]"

如何在我的对象中转换此json

class Check {String domain , String status ...}

它返回List<Check>,但Check属性为null。看我的代码。和Gson一起。

Gson gson = new Gson();
    Type fooType = new TypeToken<Collection<Check>>(){}.getType();
    System.out.println(((List<Check>)gson.fromJson("[{\"check\":{\"status\":\"2\",\"domain\":\"william.com.br\"}}]", fooType)).get(0).getDomain());

当我调试返回的列表时,它包含列表中的所有对象,但所有属性都为null。

有什么问题?

2 个答案:

答案 0 :(得分:2)

您需要一个自定义转换器,因为您有一个包含名为check的属性的对象列表。此属性属于具有domainstatus属性的类。

这里有两种可能性:

  1. 如果您不想更改Json格式,或者您根本无法编写自己的JsonDeserializer,您将在其中实例化Check对象然后自己设置属性是最好的选择;
  2. 或者您可以修改您的支票类,以便拥有包含名为check的属性和另一个domain的属性的status属性。
  3. 对于第二种情况非常清楚需要做什么,但对于第一种情况,你可以做类似的事情:

    import java.lang.reflect.Type;
    import java.util.Collection;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    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 GsonTestClass {
    
        static class MyDeserializer implements JsonDeserializer<Check> {
    
            public Check deserialize(JsonElement arg0, Type arg1,
                    JsonDeserializationContext arg2) throws JsonParseException {
                JsonObject jsonObject = arg0.getAsJsonObject().get("check").getAsJsonObject();
                // this code could be improved with null checks and so on...
                return new Check( //
                        jsonObject.get("domain").getAsString(), //
                        jsonObject.get("status").getAsString() //
                );
            }
    
        }
    
        public static void main(String args[]) {
            Gson gson = new GsonBuilder().registerTypeAdapter(Check.class, new MyDeserializer()).create();
            String json = "[{\"check\":{\"status\":\"2\",\"domain\":\"william.com.br\"}}]";
            Type fooType = new TypeToken<Collection<Check>>() {}.getType();
            System.out.println((gson.fromJson(json, fooType)));
        }
    
        static class Check {
    
            private String domain;
            private String status;
    
            public Check() {
            }
    
            public Check(String domain, String status) {
                super();
                this.domain = domain;
                this.status = status;
            }
    
            public String getDomain() {
                return domain;
            }
    
            public void setDomain(String domain) {
                this.domain = domain;
            }
    
            public String getStatus() {
                return status;
            }
    
            public void setStatus(String status) {
                this.status = status;
            }
    
            @Override
            public String toString() {
                return "Check: " + domain + " - " + status;
            }
    
        }
    
    }
    

答案 1 :(得分:2)

您的Java类Check及其字段domainstatus对应

{"status":"2","domain":"william.com.br"}
在JSON中

,因此List<Check>的JSON“等效”将是

[{"status":"1","domain":"qwe.coedu.br"},{"status":"1","domain":"qwe.com.br"}]

您的JSON具有另一级别的对象嵌套,其中每个列表条目都是一个对象,其中一个属性名为check。要么重构JSON以删除看似不必要的嵌套,要么反序列化为例如。

List<Map<String,Check>>