org.json - JSONObject
La clase JSONObject es una colección desordenada de pares clave-valor. Proporciona métodos para acceder a los valores por clave y poner valores. Se admiten los siguientes tipos:
Boolean
JSONArray
JSONObject
Number
String
Objeto JSONObject.NULL
Ejemplo
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", "Robert");
jsonObject.put("ID", 1);
jsonObject.put("Fees", new Double(1000.21));
jsonObject.put("Active", new Boolean(true));
jsonObject.put("Other Details", JSONObject.NULL);
JSONArray list = new JSONArray();
list.put("foo");
list.put(new Integer(100));
jsonObject.put("list",list);
System.out.println(jsonObject);
}
}
Salida
{"Active":true,"Other Details":null,"ID":1,"Fees":1000.21,"list":["foo",100],"Name":"Robert"}