2014-03-31 57 views
3

嗨,我正在开发小型应用程序,我试图在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&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 
] 

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

回答

1

定义sectionChords,并sectionCordData *为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); 
+0

真棒好友。但是,你能解释一下为什么这样吗? – nilkash

+0

它应该能够根据API生成数组(如您所期望的那样):JSONObject.put(java.lang.String key,java.util.Collection value)。然而,看起来像你正在使用的图书馆没有做到嵌套收集。你可以使用自定义类来代替内部列表。 – vadchen

+0

@nilkash Btw,http://jettison.codehaus.org/能够使用您的代码 – vadchen

1

必须做你根据docs想要什么。

将一个键/值对放入JSONObject中,其中值将是从集合生成的JSONArray。

但它把你的对象的JSON表示为字符串。所以你应该使用JSONArrays或Java对象。你不应该混合它们。

0

控制器:

public JsonResult Index() 
     { 
      List<Location> location = (from a in db.Locations select a).ToList(); 
      return Json(location, JsonRequestBehavior.AllowGet); 
     } 
+0

为什么这段代码应该工作的任何解释? – VPK