Espacio de tecnologia, software libre y sus derivados. Una horda de monos entrenados escriben de vez en cuando por aqui algunas noticias, opiniones e incluso alguna que otra cosa fuera del tema. Maqueros, favor de abstenerse que no somos lo suficientemente guapos.

Piano daemon

De JSON a java

Para vivir tengo que escribir kilometros y kilometros de codigo en este mugre lenguaje (oseace java)….
Para que ustedes no pasen por lo mismo… ahi les dejo algo hecho para pasar de JSON a java


import java.util.List;
import java.util.ArrayList;

import java.util.Map;
import java.util.Iterator;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.util.HashMap;

public class Json2Java {

	/**
	* getList provides a List representation of the JSON Object
	* @param jsonResponse The JSON array string
	* @return List of JSONObject.
	**/
	public List getList(String jsonResponse) throws Exception {
	  List listResponse = new ArrayList();
	  if (jsonResponse.startsWith(”[”)) {
	    JSONArray jsonArray = JSONArray.fromObject(jsonResponse);
	    toJavaList(jsonArray, listResponse);
	  } else {
	    throw new Exception(”MalFormed JSON Array Response.”);
	  }

	  return listResponse;
	}

	/**
	* getMap provides a Map representation of the JSON Object
	* @param jsonResponse The JSON object string
	* @return Map of JSONObject.
	**/
	public Map getMap(String jsonResponse ) throws Exception {
	  Map mapResponse = new HashMap();
	  if (jsonResponse.startsWith(”{”)) {
	    JSONObject jsonObj = JSONObject.fromObject(jsonResponse);
	    toJavaMap(jsonObj, mapResponse);
	  } else {
	    throw new Exception(”MalFormed JSON Array Response.”);
	  }
	  return mapResponse;
	}

	/**
	* toJavaMap converts the JSONObject into a Java Map
	* @param o
	* JSONObject to be converted to Java Map
	* @param b
	* Java Map to hold converted JSONObject response.
	**/
	@SuppressWarnings(”unchecked”)
	private static void toJavaMap(JSONObject o, Map b) {
	  Iterator ji = o.keys();
	  while (ji.hasNext()) {
	    String key = (String) ji.next();
	    Object val = o.get(key);
	    if (val.getClass() == JSONObject.class) {
	      Map sub = new HashMap();
	      toJavaMap((JSONObject) val, sub);
	      b.put(key, sub);
	    } else if (val.getClass() == JSONArray.class) {
	      List l = new ArrayList();
	      JSONArray arr = (JSONArray) val;
	      for (int a = 0; a < arr.size(); a++) {
	        Map sub = new HashMap();
	        Object element = arr.get(a);
	        if (element instanceof JSONObject) {
	          toJavaMap((JSONObject) element, sub);
	          l.add(sub);
	        } else {
	          l.add(element);
	        }
	      }
	      b.put(key, l);
	    } else {
	      b.put(key, val);
	    }
	  }
	}

	/**
	* toJavaList converts JSON’s array response into Java’s List
	* @param ar
	* JSONArray to be converted to Java List
	* @param ll
	* Java List to hold the converted JSONArray response
	**/
	private static void toJavaList(JSONArray ar, List ll) {
	  int i = 0;
	  while (i < ar.size()) {
	    Object val = ar.get(i);
	    if (val.getClass() == JSONObject.class) {
	      Map sub = new HashMap();
	      toJavaMap((JSONObject) val, sub);
	      ll.add(sub);
	    } else if (val.getClass() == JSONArray.class) {
	      List l = new ArrayList();
	      JSONArray arr = (JSONArray) val;
	      for (int a = 0; a < arr.size(); a++) {
	        Map sub = new HashMap();
	        Object element = arr.get(a);
	        if (element instanceof JSONObject) {
	          toJavaMap((JSONObject) element, sub);
	          ll.add(sub);
	        } else {
	          ll.add(element);
	        }
	      }
	      l.add(l);
	    } else {
	      ll.add(val);
	    }
	    i++;
	  }
	}

}

						

			

Comenta