Created
October 16, 2012 12:11
-
-
Save relax-more/3898895 to your computer and use it in GitHub Desktop.
Jackson で List<Map<String, Object>> にJsonデータをマッピングするサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.snippet.service; | |
import static org.junit.Assert.*; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.type.TypeReference; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.mockito.InjectMocks; | |
import org.mockito.Mock; | |
import org.mockito.MockitoAnnotations; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class JacksonComplexObjectMappiongEx{ | |
@Test | |
public void convertJsonStringToListMap() { | |
String jsonString = | |
"[{\"id\":\"registered_user\", \"country_code\":\"JP\", \"app_type\":\"ANDROID\"}" + | |
",{\"id\":\"registered_user\", \"country_code\":\"KO\", \"app_type\":\"ANDROID\"}" + | |
",{\"id\":\"registered_user\", \"country_code\":\"US\", \"app_type\":\"ANDROID\"}]"; | |
try { | |
List<Map<String, Object>> inputs = mapper.readValue(jsonString, new TypeReference<ArrayList<HashMap<String, Object>>>() { | |
}); | |
for (Map<String, Object> input : inputs) { | |
LOGGER.debug(input.toString()); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
fail(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment