嗨,我正在開發小型應用程序,我試圖在http post方法中傳遞一些數據。所以我想發送我的數據這樣的形式如何傳遞json對象中的對象列表
"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
所以我的問題是關於部分參數。我正在做這部分參數。
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§ion=[
{
"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
]
如何將它作爲列表而不是字符串發送。需要幫忙。謝謝。
真棒好友。但是,你能解釋一下爲什麼這樣嗎? – nilkash
它應該能夠根據API生成數組(如您所期望的那樣):JSONObject.put(java.lang.String key,java.util.Collection value)。然而,看起來像你正在使用的圖書館沒有做到嵌套收集。你可以使用自定義類來代替內部列表。 – vadchen
@nilkash Btw,http://jettison.codehaus.org/能夠使用您的代碼 – vadchen