Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package json;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class Main
- {
- public static String CLAVE_NOMBRE ="nombre";
- public static void main(String[] args)
- {
- try
- {
- //jsonObject();
- //jsonArray();
- //getJsonObject();
- decodeJsonObject();
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void decodeJsonObject() throws JSONException
- {
- JSONObject jsonObject = new JSONObject(getJsonObject());//{ }
- //JSONArray jsonArray = new JSONArray(getJsonObject());// ESTA MAL // [ ]
- Persona persona = new Persona();
- if ((jsonObject.has("activo"))&&(!jsonObject.isNull("activo")))
- {
- System.out.println(jsonObject.getBoolean("activo"));
- persona.setActivo(jsonObject.getBoolean("activo"));
- }
- else
- System.out.println("clave mal");
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2 = jsonObject.getJSONObject("direccion");
- System.out.println(jsonObject2.getString("calle"));
- JSONArray jsonArray = new JSONArray();
- jsonArray = jsonObject.getJSONArray("numeros");
- for (int i=0;i<jsonArray.length();i++)
- {
- JSONObject jsonObjectArray = jsonArray.getJSONObject(i);
- System.out.println(jsonObjectArray.getString("tipo"));
- System.out.println(jsonObjectArray.getString("numero"));
- }
- }
- public static JSONObject jsonObject() throws JSONException
- {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("activo", true);
- jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
- jsonObject.put("edad", 31);
- jsonObject.put("altura", 1.88);
- jsonObject.put("nombre", "john lennon");
- jsonObject.put("numeros", jsonArray2());
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2.put("calle", "calle falsa");
- jsonObject2.put("numero", 123);
- jsonObject.put("direccion", jsonObject2);
- //System.out.println(jsonObject.toString());
- return jsonObject;
- }
- public static String getJsonObject() throws JSONException
- {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("activo", true);
- jsonObject.put(CLAVE_NOMBRE, "gonzalo benoffi");
- jsonObject.put("edad", 31);
- jsonObject.put("altura", 1.88);
- jsonObject.put("nombre", "john lennon");
- jsonObject.put("numeros", jsonArray2());
- JSONObject jsonObject2 = new JSONObject();
- jsonObject2.put("calle", "calle falsa");
- jsonObject2.put("numero", 123);
- jsonObject.put("direccion", jsonObject2);
- System.out.println(jsonObject.toString());
- return jsonObject.toString();
- }
- public static void jsonArray() throws JSONException
- {
- JSONArray array = new JSONArray();
- array.put(1);
- array.put(2);
- array.put(3);
- //System.out.println(array.toString());
- JSONArray array2 = new JSONArray();
- array2.put(jsonObject());
- array2.put(jsonObject());
- array2.put(jsonObject());
- //System.out.println(array2.toString());
- }
- public static JSONArray jsonArray2() throws JSONException
- {
- JSONArray array = new JSONArray();
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("tipo", "trabajo");
- jsonObject.put("numero", "223123456");
- array.put(jsonObject);
- array.put(jsonObject);
- array.put(jsonObject);
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment