简体   繁体   English

如果json可能具有不同的结构,如何将json转换为java对象

[英]How to convert json to java object if the json may have differenct structure

I want to use java to make read return value from a url. 我想使用Java从URL读取返回值。 The response json may be 响应json可能是

{
   token:"blablablabla",
   expired: 7200
}

or of the form: 或形式:

{
    errcode: 201
    errmsg: "out of limited"
}

I currently used jackson to convert json to java object. 我目前使用jackson将json转换为java对象。 Yet in my condition, the response json may be different. 但是在我的情况下,响应json可能有所不同。 All the example and tutorial I had found deal with a json string of only one form. 我发现的所有示例和教程都只处理一种形式的json字符串。

I would like to know what is the best practice when dealing with this situation.Currently I just catch (JsonMappingException e) and try again to convert to a different class. 我想知道处理这种情况的最佳实践。当前,我只是catch (JsonMappingException e)然后再次尝试转换为其他类。

The correct answer would be for the response to carry a clear marking that indicates what class it's intended to represent. 正确的答案是使响应带有清晰的标记,以指示要表示的类别。

Without that, you're stuck with either trying things at random, or coming up with an alternative loading mechanism. 否则,您将要么尝试随机尝试,要么想出另一种加载机制。

One approach might be to read the JSON into a tree or map or other tagged-data structure and then uses reflection to try to find a class whose fields match the field names given by the JSON... which would be a rather painful and fragile way to approach the problem. 一种方法可能是将JSON读取到树或地图或其他带标签的数据结构中,然后使用反射来尝试找到其字段与JSON给定的字段名称相匹配的类……这将是非常痛苦且脆弱的解决问题的方法。

Or you can skip the latter stage and just work with the abstract data structure, much as XML users work with the Document Object Model. 或者,您可以跳过后面的阶段,而仅使用抽象数据结构,就像XML用户使用文档对象模型一样。 Of course you wouldn't have access to any of the methods on specific objects this way, and you'd still have to come up with some sort of logic that recognized enough of the content to do something useful with it... but it would let you examine and manipulate those contents without having to know a priori which object was returned. 当然,您将无法以这种方式访问​​特定对象上的任何方法,并且您仍然必须提出某种逻辑,以识别足够多的内容以对其进行有用的处理……但是它可以让您检查和处理那些内容,而不必事先知道返回了哪个对象。

From what I understand, errcode and errmsg are common response attributes. 据我了解, errcodeerrmsg是常见的响应属性。 So you may want to create an abstract class or interface like JsonResponse which holds these common attributes and have all the response classes () inherit JsonResponse . 因此,您可能想创建一个抽象类或接口,例如JsonResponse ,该类或接口包含这些公共属性,并使所有响应类()继承JsonResponse So in your code you can first check if the response has an error or not by checking these attributes. 因此,在您的代码中,您可以首先通过检查这些属性来检查响应是否有错误。

Eg 例如

public abstract Class JsonResponse {
  private String errcode;
  private String errmsg;
  //getters...
  //setters...
}

and, 和,

public class MyResponse extends JsonResponse {
  private String token;
  private String expired;
  //getters...
  //setters...
}

EDIT: Changed to 'abstract class' as corrected by Allan. 编辑:由艾伦(Allan)更正为“抽象类”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM