如何在json对象中传递对象列表

时间:2014-03-31 10:52:53

标签: json arraylist arrays

您好我正在开发小型应用程序,我试图在http post方法中传递一些数据。所以我想发送像我这样的表格

"storeid": "151",
    "floordata": {
    "entry": [
        10,
        15
    ],
    "exit": [
        10,
        15
    ],

    "section": [
        {
            "id": "0",
            "sectionname": "ABC",
            "category": "office",
           "boundary": [
                [
                    85,
                    258
                ],
                [
                    85,
                    298
                ],
                [
                    125,
                    298
                ],
                [
                    125,
                    258
                ],
                [
                    85,
                    258
                ]
            "description": "Mobile based company"
        }
    ]
    },
    "category": null,
    "description": null

所以我的问题是关于section参数。我正在为section参数执行此操作。

        String section = "section=";

        // Problem for me is here ... 
        JSONArray sections = new JSONArray();

        List<List<Float>> sectionCords = new ArrayList<List<Float>>();
        List<Float> sectionCordData = new ArrayList<Float>();
        sectionCordData.add(0.0f);
        sectionCordData.add(0.1f);

        List<Float> sectionCordData1 = new ArrayList<Float>();
        sectionCordData1.add(0.0f);
        sectionCordData1.add(0.1f);

        sectionCords.add(sectionCordData);
        sectionCords.add(sectionCordData1);

        JSONObject sectionObj = new JSONObject();
        //List<JSONObject> cordList = new ArrayList<JSONObject>();

        try {
            sectionObj.put("category", "office");
            sectionObj.put("description", "Mobile based company");
            sectionObj.put("sectionname", "mobiotics");
            sectionObj.put("id", 0);
            sectionObj.put("boundary", sectionCords); // Check Here i am sending as list ...

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sections.put(sectionObj);
        section += sections;

        section +="&";

但实际上我的部分边界参数以字符串而不是列表的形式出现。喜欢这个

 boundary=[
  [
    0.0,
    0.1
  ],
  [
    0.0,
    0.1
  ]
],storeid=156&floor=2&section=[
  {
    "id": 0,
    "sectionname": "mobiotics",
    "category": "office",
    "boundary": "[[0.0, 0.1], [0.0, 0.1]]", // See here is my problem ...
    "description": "Mobile based company"
  }
],entry=[
  10,
  15
],exit=[
  10,
  15
]   

如何将其作为列表而不是字符串发送。需要帮忙。谢谢。

3 个答案:

答案 0 :(得分:1)

sectionChordssectionCordData *定义为JSONArray而不是List:

JSONArray sectionChords = new JSONArray();
    JSONArray sectionCordData = new JSONArray();
    sectionCordData.put(0.0f);
    sectionCordData.put(0.1f);

    JSONArray sectionCordData1 = new JSONArray();
    sectionCordData1.put(0.0f);
    sectionCordData1.put(0.1f);

    sectionCords.put(sectionCordData);
    sectionCords.put(sectionCordData1);

答案 1 :(得分:1)

根据docs,它必须做你想做的事。

  

将一个键/值对放在JSONObject中,其中值将是从Collection生成的JSONArray。

但是它将对象的JSON表示形式设置为 String 。所以你应该使用JSONArrays或Java对象。你不应该混合它们。

答案 2 :(得分:0)

控制器:

document.properties.content.write(pdf.properties.content);