简体   繁体   中英

Communication JSON between Java and Node.js

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)
    }

});

You are overriding the json array items in your for loop, so in the end you will have only attributes from the last Articoli, try to create a json object for each Articoli item and put it in the json array

JSONObject obj = new JSONObject();
        obj.put("Note", note);
        JSONArray objArticoli=new JSONArray();
        for(int i=0; i<Articoli.size(); i++)
        {
            JSONObject articloliItem = new JSONObject();
            articloliItem.put("Codice", Articoli.get(i).GetCodice());
            articloliItem.put("Descrizione", Articoli.get(i).GetDescrizione());
            articloliItem.put("Prezzo", Articoli.get(i).GetPrezzo());
            objArticoli.put(articloliItem);

        }
        obj.put("Articoli",objArticoli);

This will result in a json object having the following structure

{
"Note": "some note",
"Articoli":[{
                "Codice": "CodiceValue 1",
                "Descrizione": "DescrizioneValue 1",
                "Presso": "Prezzo 1"
            },
            {
                "Codice": "CodiceValue 2",
                "Descrizione": "DescrizioneValue 2",
                "Presso": "Prezzo 2"
            }]
}

In node.js code you are trying to parse only the first property of the request json body. Usually the correct approach consists in parsing the whole request body and then working with the resulting object. Could you try in this way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM