以JSON结构发送2d数组

时间:2013-12-17 17:43:20

标签: android arrays json

我想通过Android应用程序以JSON结构发送2D数组。

我想要的整体结构是

{
                      "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]
                                      ]
                  }
         }

我想要从游标数据生成的2D数组并放入此JSON结构

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

可以解决什么问题?

1 个答案:

答案 0 :(得分:9)

此代码:

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();

会生成此字符串:

{
    "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"
    }
}