Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class Main {
- public static void main(String[] args)
- {
- JSONtoJava(devolverJSON());
- }
- public static void JSONtoJava(String json)
- {
- try
- {
- //JSONArray array = new JSONArray(json);
- JSONObject jsonObject = new JSONObject(json);
- System.out.println(jsonObject.getString("apellido"));
- JSONObject jsonObject_tel;
- JSONArray telefonos = jsonObject.getJSONArray("telefonos");
- for (int i = 0; i<telefonos.length();i++)
- {
- jsonObject_tel = telefonos.getJSONObject(i);
- System.out.println(jsonObject_tel.getString("num"));
- System.out.println(jsonObject_tel.getBoolean("esFijo"));
- }
- String direccion = jsonObject.getJSONObject("direccion").getString("calle");
- System.out.println(direccion);
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public static String devolverJSON()
- {
- String jsonToString = "";
- try
- {
- JSONObject miPrimerObjetoJSON = new JSONObject();
- JSONObject unaDireccion = new JSONObject();
- JSONArray misTelefonos = new JSONArray();
- JSONObject unTelefono = new JSONObject();
- unTelefono.put("num", "2235161198");
- unTelefono.put("esFijo", false);
- JSONObject otroTelefono = new JSONObject();
- otroTelefono.put("num", "2234708963");
- otroTelefono.put("esFijo", true);
- misTelefonos.put(unTelefono);
- misTelefonos.put(otroTelefono);
- unaDireccion.put("calle", "salta");
- unaDireccion.put("numero", 4021);
- miPrimerObjetoJSON.put("edad",32);
- miPrimerObjetoJSON.put("nombre", "gonzalo");
- miPrimerObjetoJSON.put("apellido", "benoffi");
- miPrimerObjetoJSON.put("trabaja", true);
- miPrimerObjetoJSON.put("peso", 88.5);
- miPrimerObjetoJSON.put("direccion", unaDireccion);
- miPrimerObjetoJSON.put("telefonos",misTelefonos);
- jsonToString = miPrimerObjetoJSON.toString();
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- return jsonToString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement