我已经有一些关于这个问题的帖子。自从上次我清理我的代码以编写JSON后,现在看起来更好更容易阅读。问题是我不能阅读它。
[
{},
{
"Cliente0": {
"cif": "45417265",
"metodopago": "T",
"codrefcli": "1541121",
"razonsocial": "Perico de los palotes",
"num": "964491837",
"correo": "perico@yahoo.com",
"direccion": "calle maria la vieja",
"observaciones": "que pesado"
}
},
{
"Empleado0": {
"apellidos": "Basilio",
"codempleado": "1",
"fechaalta": "2018-05-17",
"numcuenta": "884723877",
"direccion": "Calle el rio",
"copiacontrato": "aaa",
"nombre": "Carlos",
"dni": "15421451H"
}
},
{},
{
"PedidoCliente0": {
"numped": "454524",
"fechapedido": "2018-05-09",
"codcliente": "1541121",
"cpp": "5132413",
"enviadoa": "Perico de los palotes",
"formapago": "Efectivo"
}
},
{
"PedidoProveedor0": {
"numped": "7455112",
"fechapedido": "2018-05-01",
"codcliente": "10",
"cpp": "4555424",
"enviadoa": "Calle el marino",
"formapago": "Efectivo"
}
},
{
"Producto0": {
"existencias": "10",
"crf": "4555424",
"name": "Limpiador",
"pvp": "14.99",
"pve": "8.44",
"cdp": "10",
"familia": "Limpieza",
"desc": "Producto de importacion italiano"
},
"Producto1": {
"existencias": "100",
"crf": "5132413",
"name": "Colador",
"pvp": "18",
"pve": "15",
"cdp": "10",
"familia": "Cocina",
"desc": "Colador de acero"
}
},
{},
{
"Proveedor0": {
"cif": "10",
"metodopago": "1",
"crf": "10",
"razonsocial": "pene",
"num": "10",
"correo": "10",
"direccion": "10",
"observaciones": "10"
},
"Proveedor1": {
"cif": "45411",
"metodopago": "T",
"crf": "5421452",
"razonsocial": "Falete",
"num": "42314",
"correo": "falete@gmail.com",
"direccion": "tu casa",
"observaciones": "Es muy pesado"
}
}
]
我想从“Proveedor”中读取cif,但我不知道如何阅读它。我尝试了很多方法,但没有成功。如果有人能写出正确的代码,请
直到现在我试过的代码
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
import org.json.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class parse {
routes router = new routes();
String ruta = router.getruta();
public parse() {
// TODO Auto-generated constructor stub
}
public void parseproductos() {
JSONParser parser = new JSONParser();
try {
try(final Scanner sc = new Scanner( new File("N:\\data.json"), "UTF-8" )) {
final String fileContents = sc.useDelimiter("\\A").next();
JSONObject rootJSON = (JSONObject) new JSONParser().parse(fileContents);
JSONArray productos = (JSONArray) rootJSON.get("6");
int i = 0;
for(Object projectObj: productos.toArray()) {
JSONObject prod = (JSONObject)projectObj;
JSONArray datosprod = (JSONArray) prod.get("Producto"+i);
for(Object aprodObj: datosprod.toArray()){
JSONObject dataObj = (JSONObject) aprodObj;
System.out.println(dataObj);
}
}
}
}
catch (FileNotFoundException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace();}
catch (ParseException e) { e.printStackTrace();}
catch (Exception e) { e.printStackTrace();}
}
这是我得到的错误
java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
at parse.parseproductos(parse.java:24)
at maine.<init>(maine.java:60)
at request$2.actionPerformed(request.java:69)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
您的代码应该是这样的:
...
final String fileContents = sc.useDelimiter("\\A").next();
//System.out.println("FFFF " + fileContents);
JSONArray arr = new JSONArray(fileContents);
JSONObject prov = (JSONObject) arr.get(8);
System.out.println("arr " + arr.toString());
for (int i = 0; i < prov.length(); i++) {
JSONObject provI = (JSONObject) prov.get("Proveedor" + i);
System.out.println(provI.get("cif"));
}
...
答案 1 :(得分:0)
最后,我感谢NiVeR回答。
public void parseproductos() {
JSONParser parser = new JSONParser();
try {
try(final Scanner sc = new Scanner( new File("N:\\data.json"), "UTF-8" )) {
final String fileContents = sc.useDelimiter("\\A").next();
JSONArray rootJSON = (JSONArray) new JSONParser().parse(fileContents);
JSONArray productos = (JSONArray) rootJSON.get(6);
for (int i = 0; i < productos.size(); i++) {
JSONObject lpro = (JSONObject) productos.get(i);
System.out.println(lpro.get("crf"));
}
}
}
catch (FileNotFoundException e) { e.printStackTrace();}
catch (IOException e) { e.printStackTrace();}
catch (ParseException e) { e.printStackTrace();}
catch (Exception e) { e.printStackTrace();}
}
}
问题在于我正在阅读相反的JSON。首先它是一个数组,然后是所有对象。