Last active
August 29, 2015 14:06
-
-
Save adityasatrio/fc8e63319ea18e7e4f92 to your computer and use it in GitHub Desktop.
Example Json Mapper (jackson) - Json Array without Class Object Description
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 com.mycompany.examplejsonmapper.entity; | |
/** | |
* | |
* @author asn | |
*/ | |
public class DikUmum { | |
private String dikUmum, tahun; | |
public String getDikUmum() { | |
return dikUmum; | |
} | |
public void setDikUmum(String dikUmum) { | |
this.dikUmum = dikUmum; | |
} | |
public String getTahun() { | |
return tahun; | |
} | |
public void setTahun(String tahun) { | |
this.tahun = tahun; | |
} | |
} |
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 com.mycompany.examplejsonmapper; | |
import com.mycompany.examplejsonmapper.entity.DikUmum; | |
import java.io.IOException; | |
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.map.type.TypeFactory; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class ExampleJsonMapper { | |
public static void main(String[] args) throws IOException { | |
Map<String, String> jsonMap = new HashMap<>(); | |
ObjectMapper objectMapper = new ObjectMapper(); | |
String jsonStringArray = "[{\"c_dikum\":\"SMA\",\"c_tahun\":\"2009\"},{\"c_dikum\":\"SMP\",\"c_tahun\":\"2006\"},{\"c_dikum\":\"SD\",\"c_tahun\":\"2003\"}]"; | |
List<HashMap> list = objectMapper.readValue(jsonStringArray, TypeFactory.defaultInstance().constructCollectionType(List.class, jsonMap.getClass())); | |
List<DikUmum> DikUmumList = new ArrayList<>(); | |
for (HashMap convertedToHashMap : list) { | |
DikUmum dikUmum = new DikUmum(); | |
dikUmum.setDikUmum(convertedToHashMap.get("c_dikum").toString()); | |
dikUmum.setTahun(convertedToHashMap.get("c_tahun").toString()); | |
DikUmumList.add(dikUmum); | |
} | |
System.out.println("convertedToHashMap" + DikUmumList); | |
for (DikUmum dikUmum : DikUmumList) { | |
System.out.println("dikUmum.getDikUmum() "+dikUmum.getDikUmum()); | |
System.out.println("dikUmum.getTahun() "+dikUmum.getTahun()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Json Mapper (jackson) - Json Array without Class Object Description
example json object :
[{"c_dikum":"SMA","c_tahun":"2009"},{"c_dikum":"SMP","c_tahun":"2006"},{"c_dikum":"SD","c_tahun":"2003"}]