error reading JSON

0

I have a code to read Json, in my test environment it works 100%, but when I change to read another Json with other settings it accuses

Unexpected token COLON(:) at position 45.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at read.ReadJSONExample.main(ReadJSONExample.java:31)

Here is the code, with the commented test

package read;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.fazecast.jSerialComm.SerialPort;

import biblioteca.Atalho;
import conexao.StartUp;
import tradutor.Recognition;

public class ReadJSONExample {

    static Atalho atalho = new Atalho();
    static Recognition recognition = new Recognition();
    StartUp su = new StartUp();

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException, InterruptedException {

        JSONParser jsonParser = new JSONParser();

        try (FileReader reader = new FileReader("data.json")) {

            Object obj = jsonParser.parse(reader);

            JSONArray PersonList = (JSONArray) obj;
            System.out.println(PersonList);


            PersonList.forEach(per -> parsePersonObject((JSONObject) per));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

//      if (isVariavelNull(atalho.getNome())) {
        if (isVariavelNull(recognition.getPredictedLabel())) {
            // -----codigo de comunicacao serial------------------------------------------------------
            // Modificar ("COMx") sendo x o valor da porta que o arduino esta
            // conectado
            SerialPort sp = SerialPort.getCommPort("COM5");
            sp.setComPortParameters(9600, 8, 1, 0);
            sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);

            if (sp.openPort()) {
                System.out.println("Porta aberta");
            } else {
                System.out.println("Porta nao aberta");
                return;
            }

            Thread.sleep(1500);
            for (Integer i = 0; i <= 5; ++i) {
                System.out.println("piscada: " + i);

                mandarSinalSerial(sp, 0);//high
                // manda o (0) para o serial
                // liga o led 1
//------pisca led
                mandarSinalSerial(sp, 2);//high
                mandarSinalSerial(sp, 3);//low
//------                
                Thread.sleep(1000);
            }
            mandarSinalSerial(sp, 1);//low

            if (sp.closePort()) {
                System.out.println("Porta fechada");
            } else {
                System.out.println("porta não fechada");
            }
            return;
        }
    }

    private static void mandarSinalSerial(SerialPort sp, Integer comando) throws IOException {
        sp.getOutputStream().write(comando);
        sp.getOutputStream().flush();
    }
    // --------------------------------------------------------------------------------------

    private static void parsePersonObject(JSONObject person) {
//      Atalho atalho = new Atalho();
        Recognition recognition = new Recognition();

        JSONObject PersonObject = (JSONObject) person.get("Person");

//      atalho.setNome((String) PersonObject.get("nome"));
        recognition.setPredictedLabel((String) PersonObject.get("StreamLabel"));

//      atalho.setSobrenome((String) PersonObject.get("sobrenome"));
        recognition.setConfidence((Double) PersonObject.get("Confidence"));

//      System.out.printf(atalho.getNome());
//      System.out.println(" "+atalho.getSobrenome());
        System.out.printf(recognition.getPredictedLabel());
        System.out.println(" " + recognition.getConfidence());
    }

    public static boolean isVariavelNull(String s) {
        return s == null;
    }

}

Test Json

[{"Person":{"nome":"Aaaaa","sobrenome":"Bbbbb"}}]

Json that gives error

{"stream_label":"INHVP1",
"people":[
"top_left":{"x":984,"y":422},
"recognition":{"predictedLabel":"Aaaaa Bbbbb",
"confidence":50.20001732681656},
"bottom_right":{"x":1117,"y":623}}]}

The error is pointing out that Json is giving error, how do I wrap this error? and is it generated in this way?

    
asked by anonymous 30.11.2018 / 17:20

2 answers

0

"I solved my problem, instead of interpreting json as json I used the split, because a json" is "a txt ... it worked

The logic behind it is:

  • Have you identified a person? how many%?

  • If you identified a led1 lights up

  • If you did not identify a led2

    package read;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    import com.fazecast.jSerialComm.SerialPort;
    
    import tradutor.Pessoa;
    
    public class JoinSplit {
    
    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException, InterruptedException {
    try {
        BufferedReader buffer = new BufferedReader(new FileReader("data.json"));
        String linha = "";
        String separador = "},";
        while (buffer.ready()) {
            linha = buffer.readLine();
    
            String[] dados = linha.split(separador);
            Pessoa pessoa = new Pessoa(dados[0]);
    
            if (buffer.readLine().contains("\"bottom_right\"")) {
    
    
                // ----------------------------------------------------------------
    
                if (isVariavelNull(Pessoa.getConfidence())) {
                    System.out.println("---------------------------------");
                    System.out.println(Pessoa.getConfidence());
                    System.out.println("---------------------------------");
                    SerialPort sp = SerialPort.getCommPort("COM5");
                    sp.setComPortParameters(9600, 8, 1, 0);
                    sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
    
                    if (sp.openPort()) {
                        System.out.println("Porta aberta");
                    } else {
                        System.out.println("Porta nao aberta");
                        return;
                    }
    
                    Thread.sleep(1000);
                    for (Integer i = 0; i <= 5; ++i) {
                        //liga led OK
                        mandarSinalSerial(sp, 0);
                        // ------
                        Thread.sleep(500);
                    }
                    // desliga led OK
                    mandarSinalSerial(sp, 1);
    
    
                    if (sp.closePort()) {
                        System.out.println("Porta fechada");
                    } else {
                        System.out.println("porta não fechada");
                    }
                    return;
                }
                // -----------------------------------------------------
            }
        } 
    
                System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                System.out.println("pessoa nao cadastrada");
                System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                SerialPort sp = SerialPort.getCommPort("COM5");
                sp.setComPortParameters(9600, 8, 1, 0);
                sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
    
                if (sp.openPort()) {
                    System.out.println("Porta aberta");
                } else {
                    System.out.println("Porta nao aberta");
                    return;
                }
    
                Thread.sleep(100);
                for (Integer i = 0; i <= 5; ++i) {
                    //liga led NOK
                    mandarSinalSerial(sp, 2);// high
                    // ------
                    Thread.sleep(500);
                }
                // desliga led NOK
                mandarSinalSerial(sp, 3);// low
    
                if (sp.closePort()) {
                    System.out.println("Porta fechada");
                } else {
                    System.out.println("porta não fechada");
                }
                return;
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    
    public static boolean isVariavelNull(String s) {
    return s != null;
    }
    
    private static void mandarSinalSerial(SerialPort sp, Integer comando) throws IOException {
        sp.getOutputStream().write(comando);
        sp.getOutputStream().flush();
    }
    
    }
    
  • 05.12.2018 / 17:06
    0

    But your JSON is wrong indeed. In array people you are setting properties when it should be a list.

    You can check that Javascript can not interpret your data with the syntax that has been defined below:

    x = {"stream_label":"INHVP1",
    "people":[
    "top_left":{"x":984,"y":422},
    "recognition":{"predictedLabel":"Aaaaa Bbbbb",
    "confidence":50.20001732681656},
    "bottom_right":{"x":1117,"y":623}}]};
        
    30.11.2018 / 17:23