Add data to JSONArray comparing data

0

According to the case that presented before like restlessness ... Add data to JSONArray

Now, for this very thing

for (int l = 0; l < lineas.length(); l++) {
    lineas.getJSONObject(l).put("importe_aumentado", json.getJSONObject(l).getString("importe_aumentado"));
    lineas.getJSONObject(l).put("importe_hotel", json.getJSONObject(l).getString("importe_hotel"));
    lineas.getJSONObject(l).put("porcent_iva", json.getJSONObject(l).getString("porcent_iva"));
    lineas.getJSONObject(l).put("ivaInc", json.getJSONObject(l).getBoolean("ivaInc"));
}

I have to make this work when several lines come to me, I have to join my two objects but the position of my json.getJSONObject () will have to be comparing a field that I have in lineas.getJSONObject () that is called number and in json.getJSONObject () that is called line_num. This is because not necessarily the information I want to add this in the position of my json.getJSONObject () is the same as lineas.getJSONObject ().

json.getJSONObject (). getString ("num_linea"))

Must be equal to

lineas.getJSONObject (). getString ("number"))

To add the correct data.

    
asked by Kat Gar 24.09.2018 в 16:07
source

1 answer

1

Try with:

JSONArray json = obj.getJSONArray("desgloseLineas");
        JSONArray lineas = datosReserva.getJSONArray("lineas");
        for (int j = 0; j < json.length(); j++) {
            for (int l = 0; l < lineas.length(); l++) {
                if (json.getJSONObject(j).getString("num_lineas").equals(lineas.getJSONObject(l).getString("numero"))) {
                    lineas.getJSONObject(l).put("importe_aumentado", json.getJSONObject(j).getDouble("importe_aumentado"));
                    lineas.getJSONObject(l).put("importe_hotel", json.getJSONObject(j).getDouble("importe_hotel"));
                    lineas.getJSONObject(l).put("porcent_iva", json.getJSONObject(j).getString("porcent_iva"));
                    lineas.getJSONObject(l).put("ivaInc", json.getJSONObject(j).getBoolean("ivaInc"));
                }
            }
        }

Since when traveling at the same time the two JSON that I have I can compare with each one if there is the data that interests me in both with:

if (json.getJSONObject(j).getString("num_lineas").equals(lineas.getJSONObject(l).getString("numero")))
    
answered by 25.09.2018 в 12:11