¿hay algún ejemplo simple para Android de usar JSON en una serialización?JSON en Android - serialización
Gracias
¿hay algún ejemplo simple para Android de usar JSON en una serialización?JSON en Android - serialización
Gracias
Utilizamos la biblioteca gson para eso. La serialización es tan simple como llamar
new Gson().toJson(obj)
Y para deserialización,
new Gson().fromJson(jsonStr, MyClass.class);
Si se quiere evitar el uso de otra biblioteca en su proyecto Android sólo para (de) serializar JSON, que CAU uso siguiente código como Hago.
Para serializar
JSONObject json = new JSONObject();
json.put("key", "value");
// ...
// "serialize"
Bundle bundle = new Bundle();
bundle.putString("json", json.toString());
y deserializar
Bundle bundle = getBundleFromIntentOrWhaterver();
JSONObject json = null;
try {
json = new JSONObject(bundle.getString("json"));
String key = json.getString("key");
} catch (JSONException e) {
e.printStackTrace();
}
Saludos, Martin
protected void onPostExecute(String results) {
if (results!=null) {
try {
Tec tec_m=new Tec();
tec_m=new Gson().fromJson(results, Technician.class);
((AndroidActivity)activity).setData(tec_m);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hay una biblioteca simple de (de) serializar JSON, compatible con la propia JSON androide biblioteca.
// deserialize a java bean to json object
JSONObject studentJson = JsonDeer.toJson(student);
// serialize a java bean from json object
Student student1 = JsonDeer.fromJson(studentJson,Student.class);
Did que significa esto http://blog.brianbuikema.com/2010/04/android-how-to-deserialize-both-xml-and-json/ –
Podría ser que , Gracias – Waypoint