Announcement Announcement Module
Collapse
No announcement yet.
Deserializing JSON with element name containing hyphen Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deserializing JSON with element name containing hyphen

    I am writing an Android client that is getting info about a user via a REST-call (JSON).
    With Spring the result should be "automagically" returned as in instance of the User object I define, with fields having the same names as the elements in the JSON object.

    But one of the elements I am receiving has the name "user-activation-code", and of course I can not create a field with this name in a Java class since Java do not accept hyphens.

    How should this be handled?


    Code:
    		try {
    			HttpBasicAuthentication bAuth = new HttpBasicAuthentication(username,password);
    			HttpHeaders requestHeaders = new HttpHeaders();
    			requestHeaders.set("Authorization", bAuth.getHeaderValue());
    			requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    			HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    			RestTemplate restTemplate = new RestTemplate();
    
    			ResponseEntity<User> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, User.class);
    			User user= responseEntity.getBody();
    			return user;
    
    		} catch (Exception e) {
    			Log.e(TAG, e.getMessage(), e);
    		}

  • #2
    I found the answer after some more research:

    In my User class I just had to enter

    Code:
    	@JsonProperty("user-activation-code")
    		private String user_activation_code;

    Comment


    • #3
      Thanks for following up and letting us know how you resolved it!

      Comment

      Working...
      X