Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class Main {
- public static void main(String[] args) {
- /*
- String jsonResponse = JsonUtiles.leer("xboxgames");
- try
- {
- JSONArray jsonArray = new JSONArray(jsonResponse);
- for (int i=0;i<jsonArray.length();i++)
- {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- System.out.println("ID "+ jsonObject.getInt("id"));
- System.out.println("NAME: "+jsonObject.getString("name"));
- JSONArray ja_genre = jsonObject.getJSONArray("genre");
- System.out.println("GENEROS: ");
- for (int j=0;j<ja_genre.length();j++ )
- {
- String genero = ja_genre.getString(j);
- System.out.println(genero);
- }
- JSONArray ja_developers = jsonObject.getJSONArray("developers");
- System.out.println("developers: ");
- for (int j=0;j<ja_developers.length();j++ )
- {
- String developer = ja_developers.getString(j);
- System.out.println(developer);
- }
- System.out.println("Fechas de lanzamiento");
- JSONObject jo_releaseDates = jsonObject.getJSONObject("releaseDates");
- System.out.println("Japan :"+jo_releaseDates.getString("Japan"));
- System.out.println("NorthAmerica: "+jo_releaseDates.getString("NorthAmerica"));
- System.out.println("Europe: "+jo_releaseDates.getString("Europe"));
- System.out.println("Australia: "+jo_releaseDates.getString("Australia"));
- System.out.println();
- }
- }catch (JSONException ex)
- {
- System.out.println("JSON mal formado");
- }
- */
- Persona persona = new Persona();
- persona.setServicio(Servicio.TERRESTE);
- persona.setNombre("Gonzalo");
- JSONArray jsonArray = new JSONArray();
- JSONObject jo_persona = new JSONObject();
- try
- {
- jo_persona.put("nombre",persona.getNombre());
- JSONObject jo_servicio = new JSONObject();
- jo_servicio.put("nombreServicio",persona.getServicio().name());
- jo_servicio.put("costoServicio",persona.getServicio().getCosto());
- jo_persona.put("servicio",jo_servicio);
- jsonArray.put(jo_persona);
- // System.out.println(jo_persona.toString());
- JsonUtiles.grabar(jo_persona,"persona");
- JsonUtiles.grabar(jsonArray,"personas");
- //----------------------------------
- JSONArray jsonArray1 = new JSONArray(jsonArray.toString());
- for (int i = 0;i<jsonArray1.length();i++)
- {
- Persona persona1 = new Persona();
- JSONObject jo_persona_index = jsonArray1.getJSONObject(i);
- JSONObject jo_servicio_index = jo_persona_index.getJSONObject("service");
- String nombreServicio = jo_servicio_index.getString("nombreServicio");
- if (nombreServicio.equals("TERRESTE"))
- {
- persona1.setServicio(Servicio.TERRESTE);
- }
- }
- }
- catch (JSONException ex)
- {
- System.out.println("clave nula o valor infinito");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement