Created
April 21, 2012 16:14
-
-
Save laabroo/2438068 to your computer and use it in GitHub Desktop.
Simple Convert Json with GSON
This file contains 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
public class ConvertJakson { | |
private JsonData jsonData; | |
private Gson gson; | |
private String json; | |
public ConvertJakson() { | |
Log.i("Hasil Json : ", jsonData + ""); | |
String data = objJson(); | |
Log.i("ConvetJakson : ", data + ""); | |
} | |
public String objJson() { | |
jsonData = new JsonData(); | |
gson = new Gson(); | |
json = gson.toJson(jsonData); | |
return json; | |
} | |
} |
This file contains 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
public class JsonData { | |
private String nama = "aku"; | |
private int nim = 123; | |
@SuppressWarnings("serial") | |
private List<String> hoby = new ArrayList<String>() { | |
{ | |
add("Futsal"); | |
add("Tenis"); | |
add("Badminton"); | |
} | |
}; | |
@Override | |
public String toString() { | |
return "JsonData [nama=" + nama + ", nim=" + nim + ", hoby=" + hoby | |
+ "]"; | |
} | |
} |
This file contains 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
public class MainActivity extends Activity { | |
private ConvertJakson jakson; | |
private Gson gson; | |
private JsonData jsonData; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
gson = new Gson(); | |
jakson = new ConvertJakson(); | |
try { | |
jsonData = new JsonData(); | |
String json = jakson.objJson(); | |
jsonData = gson.fromJson(json, JsonData.class); | |
} catch (Exception exception) { | |
Log.i("MAIN : ", exception + ""); | |
} | |
Log.i("MAIN : ", jakson + ""); | |
Log.i("MAIN : ", jsonData + ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment