Я уже сделал несколько сообщений об этой проблеме.Со времени последнего я очистил свой код для написания 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"
}
}
]
Я хочу прочитать cif из "Proveedor", но я не знаю, как его читать.Я пробовал много способов, но безуспешно.Если кто-то может написать правильный код, пожалуйста.
Вот код, который я пробовал до сих пор
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)