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 ?
But his
JSON
is really wrong. Noarray
people
you are setting properties when it should be a list.– Sorack
@Sorack how I can make a change in my code to interpret it?
– JulioOtero
@Juliootero you’re getting from whom?
– Jefferson Quesado
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...
– hkotsubo
@Jeffersonquesado I’m receiving from a facial recognition software
– JulioOtero
@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
– Jefferson Quesado
@Jeffersonquesado understood, some hint of how to search this in the internet?
– JulioOtero
@Juliootero no hint xD Maybe the software documentation says something
– Jefferson Quesado
@Jeffersonquesado puts =/, if I pick up a . txt reader I can record the information in a variable to later use it?
– JulioOtero