0

I want to use java to make read return value from a url. The response json may be

{
   token:"blablablabla",
   expired: 7200
}

or of the form:

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

I currently used jackson to convert json to java object. Yet in my condition, the response json may be different. All the example and tutorial I had found deal with a json string of only one form.

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.

Allan Ruin
  • 5,229
  • 7
  • 37
  • 42

2 Answers2

0

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.

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. 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.

keshlam
  • 7,931
  • 2
  • 19
  • 33
  • I find my problem quite similar to this http://stackoverflow.com/questions/19760138/parsing-json-in-java-without-knowing-json-format?rq=1 . May this problem don't have a satifactory answer. – Allan Ruin Mar 07 '14 at 02:12
0

From what I understand, errcode and errmsg are common response attributes. 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. So in your code you can first check if the response has an error or not by checking these attributes.

E.g.

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.

  • thank you for adding detailed snippets. It's a good workaround. I think may be is hard for statically typed language like java to deal with this kind of flexible representation. – Allan Ruin Mar 07 '14 at 02:24
  • Yes, for any such 'binding/mapping' requirements (json, jaxb, jpa, etc), we must identify 'all the possible attributes' in our objects, and try to derive interfaces/abstract classes to hold the common ones [P.S. You may want to 'accept' my answer if you found this it useful]. – Rushikesh Thakkar Mar 07 '14 at 02:32
  • I know how to use stackoverflow :) I just still kind of expecting a better solution. – Allan Ruin Mar 07 '14 at 03:01
  • Thanks mate! I'm sure you do, I was just doing my duty. No obligations :) Cheers! – Rushikesh Thakkar Mar 07 '14 at 03:19
  • By the way, I think what we should use is `public abstract class` instead of `public interface` – Allan Ruin Mar 07 '14 at 03:23
  • You're right. I have corrected the snippet. Thanks! – Rushikesh Thakkar Mar 07 '14 at 03:30