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?