如何使用flexjson以优雅的方式反序列化此JSON

时间:2014-02-18 19:28:49

标签: java json wadl json-deserialization

我正在使用Oracle OPAM,它有一个REST API,它有像这样的方法

{
   "Target Collection":[
      {
     "target":{
        "uri":"https:\/\/opam_server_host:opam_ssl_port\/opam\/target\

           /9bbcbbb087174ad1900ea691a2573b61",
        "type":"ldap",
        "name":"person1-ldap",
        "host":"opam_server_host",
        "domain":"berkeley"
        "description" : "Ldap target"
     }
      },
      {
     "target":{
        "uri":"https:\/\/opam_server_host:opam_ssl_port\/opam\/target\

           /ac246a162ce948c7b1cdcc17dfc92c15",
        "type":"ldap",
        "name":"person1-ldap2",
        "host":"opam_server_host:opam_ssl_port",
        "domain":"berkeley"
        "description" : "Ldap target"
     }
      }
   ]
}

我正在使用FlexJSON反序列化这些实体。如果可以,我会使用Apache CXF为我生成bean,但问题是WADL处于无效的https证书+基本身份验证之下,让wadl2java在这些条件下工作所花费的时间比我更多喜欢花钱(太糟糕了,不是WSDL,所以我可以快速简单地从eclipse中创建存根)。

所以我使用这个繁琐的方法用flexjson来解析这个REST API

JSONDeserializer<Map<String,List<Map<String,Map<String,String>>>>> json = new JSONDeserializer<>();
Map<String,List<Map<String,Map<String,String>>>> targetCollection = json.deserialize(new InputStreamReader(content));
List<Map<String,Map<String,String>>> col = targetCollection.get("Target Collection");             Map<String,Map<String,String>> keyVal = col.get(0);
Map<String,String> targetVal = keyVal.get("target");
System.out.println(targetVal);

这显然有效,但它有很多括号让我想起了LISP。

如果我们可以改用POJO会更好(如果我可以通过某种GUI工具自动生成,那就更好了,但我知道我要求太多,我们生活在2014年)。

我知道有一些关于如何在这里映射属性的文档:http://flexjson.sourceforge.net/#Deserialization但我真的希望他们有一些真正的复杂样本(包括JSON - 解释如何在没有任何JSON示例的情况下反序列化的用法是什么文档?)

这个问题显然不是新问题,但似乎123等相关问题也在等待答案。

(P.S。除了this之外似乎有一些我可以使用的信息)

所以我的问题是:如何将这个带有FlexJSON的JSON解析为一个不仅由地图构成的结构?

2 个答案:

答案 0 :(得分:3)

没有任何Map的工作解决方案:

让我们定义类自下而上

<强> 1。目标


public class Target {

private String uri;
private String type;
private String name;
private String host;
private String domain;
private String description;

public String getUri() {
    return uri;
}

public void setUri(String uri) {
    this.uri = uri;
}

public String getType() {
    return type;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}

public String getDomain() {
    return domain;
}

public void setDomain(String domain) {
    this.domain = domain;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
}

此类表示以下JSON:

 "target": {
        "uri": "https://opam_server_host:opam_ssl_port/opam/target/9bbcbbb087174ad1900ea691a2573b61",
        "type": "ldap",
        "name": "person1-ldap",
        "host": "opam_server_host",
        "domain": "berkeley",
        "description": "Ldap target"
      }

<强> 2。 TargetWrapper:

请注意target它位于大括号内。这个课程只是包装它。

public class TargetWrapper {

    private Target target;

    public Target getTarget() {
        return target;
    }

    public void setTarget(Target target) {
        this.target = target;
    }
 }

此类表示以下JSON:

{
      "target": {
        "uri": "https://opam_server_host:opam_ssl_port/opam/target/9bbcbbb087174ad1900ea691a2573b61",
        "type": "ldap",
        "name": "person1-ldap",
        "host": "opam_server_host",
        "domain": "berkeley",
        "description": "Ldap target"
      }
    }

第3。 RestApiResponse

此类表示您的api返回的整个JSON

public class RestApiResponse {

    @JSON(name="Target Collection")
    private List<TargetWrapper> targetCollection = new ArrayList<TargetWrapper>();

    @JSON(name="Target Collection")
    public List<TargetWrapper> getTarget_Collection() {
        return targetCollection;
    }
    @JSON(name="Target Collection")
    public void setTarget_Collection(List<TargetWrapper> tc) {
        this.targetCollection = tc;
    }

}

<强> 4。我们来试试吧!

public static void main(String[] args) {


        JSONDeserializer<RestApiResponse> js = new JSONDeserializer<RestApiResponse>();


        String input="{\"Target Collection\":[{\"target\":{\"uri\":\"https://opam_server_host:opam_ssl_port/opam/target/9bbcbbb087174ad1900ea691a2573b61\",\"type\":\"ldap\",\"name\":\"person1-ldap\",\"host\":\"opam_server_host\",\"domain\":\"berkeley\",\"description\":\"Ldap target\"}},{\"target\":{\"uri\":\"https://opam_server_host:opam_ssl_port/opam/target/ac246a162ce948c7b1cdcc17dfc92c15\",\"type\":\"ldap\",\"name\":\"person1-ldap2\",\"host\":\"opam_server_host:opam_ssl_port\",\"domain\":\"berkeley\",\"description\":\"Ldap target\"}}]}";

        RestApiResponse restApiResponse=js.deserialize(input,RestApiResponse.class);

        System.out.println(new JSONSerializer()
        .exclude("*.class").deepSerialize(restApiResponse));


    }

输出:

{
  "Target Collection": [
    {
      "target": {
        "description": "Ldap target",
        "domain": "berkeley",
        "host": "opam_server_host",
        "name": "person1-ldap",
        "type": "ldap",
        "uri": "https://opam_server_host:opam_ssl_port/opam/target/9bbcbbb087174ad1900ea691a2573b61"
      }
    },
    {
      "target": {
        "description": "Ldap target",
        "domain": "berkeley",
        "host": "opam_server_host:opam_ssl_port",
        "name": "person1-ldap2",
        "type": "ldap",
        "uri": "https://opam_server_host:opam_ssl_port/opam/target/ac246a162ce948c7b1cdcc17dfc92c15"
      }
    }
  ]
}

我希望它有所帮助。

答案 1 :(得分:0)

哦,看看这个

    JSONDeserializer<Map<String,List<MetaTarget>>> j = new JSONDeserializer<>();
    List<MetaTarget> l = j.use("values.values", MetaTarget.class) //magic that means that the item inside a list (values #1) inside a map (values #2) is a MetaTarget
    .deserialize(new InputStreamReader(JSONParser.class.getResourceAsStream("sample.json")),Map.class) //have no idea what Map does here
    .get("Target Collection"); //since deserializer returns a map, use this get to return a map value, in this case, a List

    for(MetaTarget mt : l){
        System.out.println(mt.getTarget());
        mt.getTarget().getHostname(); //I can access the attribute... thanks god
    }

其中

public class MetaTarget {
    private Target target;

    public Target getTarget() {
        return target;
    }

    public void setTarget(Target target) {
        this.target = target;
    }
}

public class Target {

    /**
     * uri is the target resource URI.
     */
    private String  uri;

    /**
     * type is the target type.
     */
    private String  type;

    /**
     * hostname is the target's host name.
     */
    private String  hostname;

    /**
     * name is the target name.
     */
    private String  name;

    /**
     * org is the target's organization.
     */
    private String  org;

    /**
     * domain is the target's domain.
     */
    private String  domain;

    /**
     * description is a description of the target system.
     */
    private String  description;

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getType() {
        return type;
    }

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

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Target [uri=" + uri + ", type=" + type + ", hostname=" + hostname + ", name=" + name + ", org=" + org + ", domain=" + domain + ", description=" + description + "]";
    }

}

减少地图!