Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As a slight modification of StaxMan's answer, if it's somehow desirable to have everything that is not the <code>User</code> object in a map, Jackson provides the <a href="http://jackson.codehaus.org/1.9.2/javadoc/org/codehaus/jackson/annotate/JsonAnySetter.html" rel="nofollow">@JsonAnySetter</a> and <a href="http://jackson.codehaus.org/1.9.2/javadoc/org/codehaus/jackson/annotate/JsonAnyGetter.html" rel="nofollow">@JsonAnyGetter</a> annotations to get the job done. The following example demonstrates how this might be used.</p> <pre><code>import java.util.HashMap; import java.util.Map; import org.codehaus.jackson.annotate.JsonAnySetter; import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; import org.codehaus.jackson.annotate.JsonMethod; import org.codehaus.jackson.map.ObjectMapper; public class JacksonFoo { public static void main(String[] args) throws Exception { // {"sc":"200","msg":"something","userInfo":{"name":"n","password":"p"}} String jsonInput = "{\"sc\":\"200\",\"msg\":\"something\",\"userInfo\":{\"name\":\"n\",\"password\":\"p\"}}"; ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); Result result = mapper.readValue(jsonInput, Result.class); System.out.println(result); // output: // Result: userInfo=User: name=n, password=p, otherThings={sc=200, msg=something} } } class Result { Map otherThings = new HashMap(); User userInfo; @JsonAnySetter void addThing(String key, Object value) { otherThings.put(key, value); } @Override public String toString() { return String.format("Result: userInfo=%s, otherThings=%s", userInfo, otherThings); } } class User { String name; String password; @Override public String toString() { return String.format("User: name=%s, password=%s", name, password); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload