Hello,
I am trying to parse some json data for the first time in processing, but I meet with fail.
I want to process data fetched from arduino-cli.exe to determen to which com port an arduino is connected and what it’s board type is.
Arduino-cli parsed the data as json. It poops out an array of JSON objects as far as I understand.
Process p = launch(command);
BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream()));
while ((line = in.readLine( )) != null) { jsonData += line ; } // store all data in this string
JSONArray jsonArray = parseJSONArray(jsonData); // parse the array into separate objects.
if (jsonArray == null)
{
println("JSONArray could not be parsed");
}
else
{
println("printing com port things") ;
JSONObject port = jsonArray.getJSONObject(0);
println("printing json array element") ;
println(port) ; // so far so good, see output below for result.
println("printing port") ;
COM_PORT = port.getString("address"); // here I am trying to fetch the comport number, and this is not working.
println( COM_PORT ) ;
println("printing boardName");
boardName = port.getString("matching_boards");
println( boardName ) ;
The above code snippet yields this output.
{
"port": {
"protocol": "serial",
"address": "COM4",
"protocol_label": "Serial Port (USB)",
"label": "COM4",
"properties": {
"vid": "0x2341",
"serialNumber": "85735313333351612150",
"pid": "0x0043"
},
"hardware_id": "85735313333351612150"
},
"matching_boards": [{
"fqbn": "arduino:avr:uno",
"name": "Arduino Uno"
}]
}
What am I doing wrong? and how should I do this?
Kind regards,
Bas