JSON Java management

1

Good morning, I have to consume a json from a lading that is stored in mysql, I already get the field but when trying to read the json it generates error, the json that I consume is the following:

[{
    "label": "Nombre Completo",
    "value": "oscar",
    "identifier": "field20",
    "type": "oneLineText",
    "page": 1,
    "page_name": "",
    "width": "100%"
}, {
    "label": "Nu00famero de identificaciu00f3n",
    "value": "7747777",
    "identifier": "field21",
    "type": "oneLineText",
    "page": 1,
    "page_name": "",
    "width": "100%"
}, {
    "label": "Correo electru00f3nico",
    "value": "[email protected]",
    "identifier": "field46",
    "type": "email",
    "page": 1,
    "page_name": "",
    "width": "100%"
}, {
    "label": "Celular",
    "value": "555545454",
    "identifier": "field23",
    "type": "oneLineText",
    "page": 1,
    "page_name": "",
    "width": "100%"
}, {
    "label": "Ciudad",
    "value": "Bogota",
    "identifier": "field24",
    "type": "dropdown",
    "page": 1,
    "page_name": "",
    "options": [{
        "value": "",
        "show": "Ciudad"
    }, {
        "value": "Bogota",
        "show": "Bogota"
    }],
    "width": "100%"
}, {
    "label": "QUIERO CONOCER Mu00c1S",
    "value": "QUIERO CONOCER MÁS",
    "identifier": "field25",
    "type": "heading",
    "page": 1,
    "page_name": "",
    "width": "100%"
}, {
    "label": "",
    "value": "",
    "identifier": "field26",
    "type": "checkbox",
    "page": 1,
    "page_name": "",
    "options": [{
        "value": "Acepto la polu00edtica de privacidad",
        "show": "Acepto la polu00edtica de privacidad"
    }],
    "width": "100%"
}, {
    "label": "u00bfTienes medicina prepagada?",
    "value": "¿Tienes medicina prepagada?",
    "identifier": "field28",
    "type": "heading",
    "page": 2,
    "page_name": "",
    "width": "100%"
}, {
    "label": "fix",
    "value": "",
    "identifier": "field44",
    "type": "heading",
    "page": 2,
    "page_name": "",
    "width": "25%"
}, {
    "label": "",
    "value": "1",
    "identifier": "field27",
    "type": "checkbox",
    "page": 2,
    "page_name": "",
    "options": [{
        "value": "1",
        "show": "Su00ed"
    }, {
        "value": "2",
        "show": "No"
    }],
    "width": "50%"
}, {
    "label": "fix",
    "value": "",
    "identifier": "field45",
    "type": "heading",
    "page": 2,
    "page_name": "",
    "width": "25%"
}, {
    "label": "u00bfCuu00e1l?",
    "value": "¿Cuál?",
    "identifier": "field29",
    "type": "heading",
    "page": 2,
    "page_name": "",
    "width": "100%"
}, {
    "label": "",
    "value": "Colmedica",
    "identifier": "field19",
    "type": "oneLineText",
    "page": 2,
    "page_name": "",
    "width": "100%"
}, {
    "label": "u00bfQuu00e9 aspecto mejoraru00edas en tu plan actual de salud?",
    "value": "¿Qué aspecto mejorarías en tu plan actual de salud?",
    "identifier": "field30",
    "type": "heading",
    "page": 2,
    "page_name": "",
    "width": "100%"
}, {
    "label": "Aspectos",
    "value": "Red Médica",
    "identifier": "field31",
    "type": "dropdown",
    "page": 2,
    "page_name": "",
    "options": [{
        "value": "",
        "show": "Cobertura"
    }, {
        "value": "Red Mu00e9dica",
        "show": "Red Mu00e9dica"
    }, {
        "value": "Beneficios Adicionales",
        "show": "Beneficios Adicionales"
    }, {
        "value": "Precio",
        "show": "Precio"
    }],
    "width": "100%"
}, {
    "label": "u00bfQuu00e9 aspecto es mu00e1s relevante al elegir tu plan de salud?",
    "value": "¿Qué aspecto es más relevante al elegir tu plan de salud?",
    "identifier": "field34",
    "type": "heading",
    "page": 2,
    "page_name": "",
    "width": "100%"
}, {
    "label": "relevante",
    "value": "",
    "identifier": "field35",
    "type": "dropdown",
    "page": 2,
    "page_name": "",
    "options": [{
        "value": "",
        "show": "Red amplia de mu00e9dicos"
    }, {
        "value": "Variedad de especialidades mu00e9dicas",
        "show": "Variedad de especialidades mu00e9dicas"
    }, {
        "value": "Beneficios adicionales",
        "show": "Beneficios adicionales"
    }, {
        "value": "Precio del plan",
        "show": "Precio del plan"
    }],
    "width": "100%"
}]

I store it in a JSONArray and store it without problems, the error is when trying to return a JSONObject, I do it in the following way:

public void mensaje() throws SQLException, JSONException{

    List<JSONArray> jsons = conexionDBDao.getNewOpportunities();

    for(int i=0;i<jsons.size();i++){

        for(int x=0;x<jsons.get(i).length();x++){

           JSONObject obj = jsons.get(i).getJSONObject(x);
        }
    }
}

It generates the following Error

org.codehaus.jettison.json.JSONException: JSONArray[0] is not a JSONObject.
at org.codehaus.jettison.json.JSONArray.getJSONObject(JSONArray.java:258) ~[jettison-1.2.jar:1.2]
at com.medplus.genoma.scheduler.Scheduler.mensaje(Scheduler.java:32) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_151]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_151]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_151]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]

Updating:

The getNewOpportunities method is the following

public List<JSONArray> getNewOpportunities () throws SQLException{

     Connection conn = DriverManager.getConnection ("jdbc:mysql://XXXX.XXXX.XXXX.XXXX/db","user", "pass");


     Statement s = conn.createStatement(); 
     ResultSet rs = s.executeQuery ("SELECT content FROM db.table;");

     List<JSONArray> json = new ArrayList<>();

     while(rs.next()){

         JSONArray j = new JSONArray();

         j.put(rs.getString("content").replace("\",""));   

         json.add(j);
     }

     conn.close();

     return json;

 }

Thanks

    
asked by Jorge Reinaldo Linares Pineda 04.12.2018 в 16:12
source

1 answer

1

Greetings.

As the error indicates:

  

org.codehaus.jettison.json.JSONException: JSONArray [0] is not to JSONObject.

The data you are inserting in your JSONArray is not explicitly a JSONObject and you can see it in the library documentation: Jettison documentation .

The method you are using is put(Object value); .

If we look at your code in this line: j.put(rs.getString("content").replace("\","")); we will realize that you are passing a String and not a JSONObject , so it would be logical that when trying to call jsons.get(i).getJSONObject(x); send us this error and in fact the correct form is: jsons.get(i).getString(x);

However, as you indicated, you need a JSONObject so you should do something like this:

String val = rs.getString("content").replace("\",""); // Se obtiene la cadena de texto con el json
j.put(new JSONObject(val)); // Se construye el JSONObject a partir del String anterior

PS: It is assumed that the values of each row in your database are in the form of a JSONObject (start and end with { ... } and not a JSONArray (start and end with [ ... ]

UPDATE

As the data is read as JSONArray then you could do something like this:

JSONArray j = new JSONArray(rs.getString("content").replace("\",""));
json.add(j);
    
answered by 04.12.2018 / 21:50
source