I have to send a json from java to node.js! To do so, use the codes below! The json consists of a single Note field, and an array of a certain type Articolo! The fact is that when I print the value in the node.js I have the error below. Can you explain to me and how can I get the values from the JSON inside node.js? The strange thing is that the note field is not even printed
Error:
{ '{"Articoli":':
{ '"SADRIN 830","8 RAGGI DOPPI 8TX 8RX - ALTEZZA 3,00 MT","232.0"': '' } }
SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)
Java Code:
JSONObject obj = new JSONObject();
obj.put("Note", note);
JSONArray objArticoli=new JSONArray();
for(int i=0; i<=Articoli.size(); i++)
{
objArticoli.put(0,""+Articoli.get(i).GetCodice());
objArticoli.put(1,""+Articoli.get(i).GetDescrizione());
objArticoli.put(2,""+Articoli.get(i).GetPrezzo());
}
obj.put("Articoli",objArticoli);
try {
Database db = new Database();
ret = db.RequestArray("/rapportini/generarapportino", obj,true);
} catch (Exception ex) {
System.out.println("\n Error"+ex);
}
Node.js:
app.post("/rapportini/generarapportino",async function(request,response)
{
try
{
console.log(request.body);
var data = JSON.parse(Object.keys(request.body)[0]);
const ret=await RapportiniController.GeneraRapportino(data.Note);
response.setHeader('Content-Type', 'application/json');
response.send(JSON.stringify({ return: ret }));
}
catch(err){
console.log("Errore generazione rapportino ",err)
}
});
console.log(request.body);log exactly? Your JSON is probably invalid. You can use jsonlint to validate any json string you have when you have issues with JSON.parse().