error in reading JSON

Asked

Viewed 114 times

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 parameters 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 test commented

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 the Json is giving error, how do I outline this error? being that it is generated in this way ?

  • 2

    But his JSON is really wrong. No array people you are setting properties when it should be a list.

  • @Sorack how I can make a change in my code to interpret it?

  • @Juliootero you’re getting from whom?

  • If JSON is invalid, I think the API won’t be able to read it. You have to see who generated this wrong JSON and fix it there, I think...

  • @Jeffersonquesado I’m receiving from a facial recognition software

  • 1

    @Juliootero then, if it does generate this data, it is not generating JSON itself. You would need a softer interpreter, recognizing a distinct JSON pattern

  • @Jeffersonquesado understood, some hint of how to search this in the internet?

  • @Juliootero no hint xD Maybe the software documentation says something

  • @Jeffersonquesado puts =/, if I pick up a . txt reader I can record the information in a variable to later use it?

Show 4 more comments

2 answers

0


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

the logic behind it is :

  1. Have you identified a person? how many % ?

  2. If identified lights a led1

  3. If not identified, lights 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();
    }
    
    }
    

0

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

You can check that the Javascript cannot interpret your data with the syntax that was 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}}]};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.