Convert from JSON to string

1

This function is part of an invoice. When the invoice appears to me, it is displayed in a JSON. I would like to see the list of products like this.

product - price

product - price

product - price, unlike what it shows me is

public String ProductosFacturas() {

    float montoTotalf = 0;

    int productsize = ctOnGo.getProductArraylistsize(1);

    //Crear Array de productos del carrito para pasarlos al servidor como pedido completado
    JSONObject Pedido = new JSONObject();
    JSONArray PedidoD = new JSONArray();
    JSONObject PedidoDL = new JSONObject();

    String pCode;
    String pName;
    Integer pQty;
    float pPrice;

    try{
        for (int j=0;j< productsize;j++){
            pCode = ctOnGo.getProducts(j,1).getProductCode();
            pName = ctOnGo.getProducts(j,1).getProductName();
            pQty = ctOnGo.getProducts(j,1).getProductQty();
            pPrice = ctOnGo.getProducts(j,1).getProductPrice();

            PedidoDL = new JSONObject();
            PedidoDL.put( "barcode", pCode );
            PedidoDL.put( "nombre", pName );
            PedidoDL.put( "cantidad", pQty );
            PedidoDL.put( "precio", pPrice );
            PedidoD.put( PedidoDL);

            montoTotalf = montoTotalf + pPrice;

        }
        Pedido.put( "Factura", PedidoD);


    } catch (JSONException e) {
        e.printStackTrace();
        Toast toast = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
        toast.show();
    }

    return Pedido.toString();
}
    
asked by Andrea Valentina 08.10.2018 в 17:22
source

1 answer

0

I think that the text you are going to show it in some text view then you could use a method that uses the answer that gives you ProductsFactura that now must return a the JSON Order

public JSONObject ProductosFacturas() {

float montoTotalf = 0;

int productsize = ctOnGo.getProductArraylistsize(1);

//Crear Array de productos del carrito para pasarlos al servidor como pedido completado
JSONObject Pedido = new JSONObject();
JSONArray PedidoD = new JSONArray();
JSONObject PedidoDL = new JSONObject();

String pCode;
String pName;
Integer pQty;
float pPrice;

try{
    for (int j=0;j< productsize;j++){
        pCode = ctOnGo.getProducts(j,1).getProductCode();
        pName = ctOnGo.getProducts(j,1).getProductName();
        pQty = ctOnGo.getProducts(j,1).getProductQty();
        pPrice = ctOnGo.getProducts(j,1).getProductPrice();

        PedidoDL = new JSONObject();
        PedidoDL.put( "barcode", pCode );
        PedidoDL.put( "nombre", pName );
        PedidoDL.put( "cantidad", pQty );
        PedidoDL.put( "precio", pPrice );
        PedidoD.put( PedidoDL);

        montoTotalf = montoTotalf + pPrice;

    }
    Pedido.put( "Factura", PedidoD);


} catch (JSONException e) {
    e.printStackTrace();
    Toast toast = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
    toast.show();
}

    return Pedido;
}


public void pedidoPrint() {
     JSONObject js = ProductoFactura();

    try {
        JSONArray mostrar = js.getJSONArray("Factura");
        for (int i = 0; i < mostrar.length(); i++) {
            JSONObject objeto = mostrar.getJSONObject(i);
            String precio = objeto.getString("precio");
            String produtcto = objeto.getString("nombre");
            //AQUI YA PUEDES SETEAR CADA VALOS A UN TEXTVIEW
            texviewPrecio.setText(precio);
            texviewProducto.setText(producto);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Your main method will create the JSON and the second method will show the already readable text

    
answered by 08.10.2018 в 18:57