0

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

});
4
  • What does 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(). Commented Jun 8, 2018 at 13:37
  • What does db.RequestArray do? Commented Jun 8, 2018 at 13:39
  • @Shilly console.log(request.body) print exactly error field that I have inserted above! The strange thing is that the class for the management of json in java is that of sun! so I do not think there are any problems in that! Most likely it is something about node.js! Commented Jun 8, 2018 at 13:41
  • @bhspencer Send a json object to router of node.js( it produce a httpurlconnection to router of node.js) Commented Jun 8, 2018 at 14:11

2 Answers 2

1

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"
            }]
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried your solution: when the java loop is executed it raises this exception: java.lang.StackOverflowError overflows, as the application runs on android
java.lang.StackOverflowError means you have an infinite loop or un-braked recursive call, first fix the loop exit condition it should be i<Articoli.size() instead of i<=Articoli.size()
I have change Articoli.size(), but now I have this exception: org.json.JSONException: End of input at character 0 of
Probably one of the Articoli items attributes(Codice, Descrizione, or Presso) is null or empty, you can add null/empty checks on those attributes before adding them to the json
I check Articoli items , none of them is null or void
0

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?

3 Comments

You could explain yourself better! That is, I print the whole request at the beginning and select the first element of the array to be previewed! How should i do?
In this line of code var data = JSON.parse(Object.keys(request.body)[0]); you are creating an array with only the properties keys of the request body object and then parsing the first of them. The approach I am suggesting is to parse the whole body of the request (var data = JSON.parse(request.body);) and then extract the thing you need from the resulting object. I suggest also to better check Elgayed answer because I think there's some issue also when you are building the json.
So to prevent the array I should do: var data = JSON.parse (request.body); ---- var art = date.Articles ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.