Results 1 to 3 of 3

Thread: Deserializing JSON with element name containing hyphen

  1. #1
    Join Date
    May 2012
    Posts
    2

    Default 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. #2
    Join Date
    May 2012
    Posts
    2

    Default

    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;

  3. #3
    Join Date
    Nov 2010
    Posts
    174

    Default

    Thanks for following up and letting us know how you resolved it!
    Roy Clarkson
    Spring Mobile Projects Lead

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •