gpt4 book ai didi

java - 如何使用 Java 从 JSONObject 获取数据?

转载 作者:行者123 更新时间:2023-12-01 16:53:06 27 4
gpt4 key购买 nike

我有一个名为 inputJson 的字符串,其中包含

{"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



}



]









}

我用这种方式将字符串转换为 JSONObject

JSONObject object = new JSONObject(inputJson);

我得到了这个

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}]}

现在我需要提取信息,但我不知道该怎么做,例如我尝试这个

object.getString("elemento1");

但是我收到了这个错误

原因:org.json.JONException:找不到 JSONObject["elemento1"]

请帮帮我

最佳答案

您无法从顶层获取嵌套 JSON 对象。它就像一个树状图。需要将其转化为java对象或者逐级获取。检查this post ,很多方法。

关于java - 如何使用 Java 从 JSONObject 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61648573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com