0

I have an String called inputJson that contains

{"listPruebas": [

 

    {

      "nombrePrueba" : "pruebaA",

 

      "id" : 1,

 

      "tipoPrueba" : "PRUEBABASE1",

 

      "elementoBase" : "tipoA",

 

            "listaMarca": [

 

            {

 

                "elemento": "elemento1 ",

 

                "tipo": "ABC",

 

                "cadena": "SFSG34235WF32"

 

            },

 

            {

 

                 "elemento":"elemento2",

 

                 "tipo":"DEF",

 

                 "cadena":"DJRT64353GSDG"

 

            },

 

            {

 

                "elemento" : "elemento3",

 

                "formato ":"JPG"

 

 

            }

 

    ]},

 

    {

 

      "nombrePrueba" : "pruebaB",

 

      "id" : 2,

 

      "tipoPrueba" : "PRUEBABASE2",

 

      "elementoBase" : "imagenPrueba",

 

            "listaMarca2": [

 

               {

 

                   "elemento" : "imagen",

 

                   "tipo": "tipo5",

 

                   "cadena": "iVBORw0KGgoAAAANSUhEUgAAAgAAAA"

 

                }

 

            ]

    }

 

 

 

            ],

 

 

 

                "listaBuscar":

 

                [

 

                {

 

                               "tipoBusqueda":"busqueda1",

 

                               "id" : 1,

 

                               "operacion" : "operacion1",

 

                               "valor" : "12"

 

                },

 

                {

 

                               "tipoBusqueda":"binario",

 

                               "id" : 2,

 

                               "operacion" : "operacion2",

 

                               "valor" : "13"

 

                },

 

                {

 

                               "tipoFiltro":"numerico",

 

                               "id" : 31,

 

                               "operacion" : "MENOR_QUE",

 

                               "valor" : "1980",

 

                               "intervalo" : 1

 

                }

 

                ]

 

 

 

 

}

and I converted the String to JSONObject of this way

JSONObject object = new JSONObject(inputJson);

and I got this

jsonObject::{"listaBuscar":[{"valor":"12","id":1,"operacion":"operacion1","tipoBusqueda":"busqueda1"},{"valor":"13","id":2,"operacion":"operacion2","tipoBusqueda":"binario"},{"tipoFiltro":"numerico","intervalo":1,"valor":"1980","id":31,"operacion":"MENOR_QUE"}],"listPruebas":[{"listaMarca":[{"tipo":"ABC","elemento":"elemento1","cadena":"SFSG34235WF32"},{"tipo":"DEF","elemento":"elemento2","cadena":"DJRT64353GSDG"},{"elemento":"elemento3","formato":"JPG"}],"elementoBase":"tipoA","tipoPrueba":"PRUEBABASE1","nombrePrueba":"pruebaA","id":1},{"elementoBase":"imagenPrueba","tipoPrueba":"PRUEBABASE2","listaMarca2":[{"tipo":"tipo5","elemento":"imagen","cadena":"iVBORw0KGgoAAAANSUhEUgAAAgAAAA"}],"nombrePrueba":"pruebaB","id":2}]}

and now I need to extract information but I dont know how to do, for example I try this

object.getString("elemento1");

but I got this error

Caused by: org.json.JONException: JSONObject["elemento1"] not found

help me please

Root93
  • 141
  • 8
  • welcome to stackoveflow. Take a tour and get your first badge-https://stackoverflow.com/tour – Hemant May 07 '20 at 02:32
  • We need to know what `JSONObject` is. Then, we or you can access the api documentation and explore the methods until a method is found that works for your problem. –  May 07 '20 at 02:48

2 Answers2

0

You can't get a nest JSON object from the top level. It's like a treemap. You need to convert it into a java object or get it level by level. check this post, a lot of ways.

PatrickChen
  • 1,350
  • 1
  • 11
  • 19
0

You json contains two json arrays, fetch them as -

 JSONArray listaBuscArray = jsonObj.getJSONArray("listaBuscar");
 JSONArray listPruebasArray = jsonObj.getJSONArray("listPruebas");

Now you can process and use them as -

for(int i=0; i<listaBuscArray.length; i++){
    JSONObject obj = listaBuscArray.getJSONObject(i);

    .... your logic

}
Hemant
  • 1,403
  • 2
  • 11
  • 21