Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send 2d array in JSON structure [closed]

Tags:

java

json

arrays

I want to send 2D array in a JSON structure.

The overall structure I want is

{
    "PED": {
        "fun": "enviarPedido",
        "txtUser":"123",
        "md5Passwd": "123",                          
        "arrArticulos": [
            [50,10,5,50],
            [51,9,6.5,58.5],
            [52,8,7,56],
            [53,7,8.5,59.5]
        ]
    }
}

I want the 2D array generated from the cursor data and put into this JSON Structure

    "arrArticulos": [
        [50,10,5,50],
        [51,9,6.5,58.5],
        [52,8,7,56],
        [53,7,8.5,59.5]
    ]

What is the solution?

like image 666
Dhiraj Tayade Avatar asked Dec 08 '22 10:12

Dhiraj Tayade


1 Answers

This code:

JSONObject PED = new JSONObject();
PED.put( "fun", "enviarPedido" );
PED.put( "txtUser", "123" );
PED.put( "md5Passwd", "123" );

JSONArray articulos1 = new JSONArray();
articulos1.put( 50 );
articulos1.put( 10 );
articulos1.put( 5 );
articulos1.put( 50 );

JSONArray articulos2 = new JSONArray();
articulos2.put( 51 );
articulos2.put( 9 );
articulos2.put( 6.5 );
articulos2.put( 58.5 );

JSONArray articulos3 = new JSONArray();
articulos3.put( 52 );
articulos3.put( 8 );
articulos3.put( 7 );
articulos3.put( 56 );

JSONArray articulos4 = new JSONArray();
articulos4.put( 51 );
articulos4.put( 9 );
articulos4.put( 6.5 );
articulos4.put( 58.5 );

JSONArray arrArticulos = new JSONArray();
arrArticulos.put( articulos1 );
arrArticulos.put( articulos2 );
arrArticulos.put( articulos3 );
arrArticulos.put( articulos4 );

PED.put( "arrArticulos", arrArticulos );

JSONObject body = new JSONObject();
body.put( "PED", PED );

String json = body.toString();

Will generate this string:

{
    "PED": {
        "arrArticulos": [
            [
                50,
                10,
                5,
                50
            ],
            [
                51,
                9,
                6.5,
                58.5
            ],
            [
                52,
                8,
                7,
                56
            ],
            [
                51,
                9,
                6.5,
                58.5
            ]
        ],
        "md5Passwd": "123",
        "txtUser": "123",
        "fun": "enviarPedido"
    }
}
like image 75
James McCracken Avatar answered Dec 11 '22 11:12

James McCracken