2012-10-18 151 views
0

JSFiddle 我已經給出了一個示例,我試圖獲取JSON數據,但它不是預期的。如何發送表單數據(對象)數組作爲json

{"txtTitle":["Tribhuwan","Pankaj"],"txtName":["Dewangan","Sharma"] 
,"seGender":["Male","Male"]} 

我想提前

+0

我相信,服務器將無論如何組合重複鍵,是否有一個具體的問題,你試圖解決這種格式? – Igor

+0

我認爲你需要在預期的輸出中切換你的'[]'和'{}'。 –

+0

鑑於JSON只是一個大概的想法,請檢查鏈接 – Tribhuwan

回答

0

你想要的輸出是invalid

如果你能夠接受的

[{"txtTitle":"Tribhuwan","txtName":"Pankaj","seGender":"Male"},{"txtTitle":"Dewangan","txtName":"Sharma","seGender":"Male"}] 

輸出住然後從鏈接example這serializeObject方法可以工作

$.fn.serializeObject = function() { 
    var o = []; 
    var a = this.serializeArray(); 
    var t = {}; 
    $.each(a, function() { 

     if(t[this.name] !== undefined){ 
      o.push(t); 
      t = {}; 
     } 

     t[this.name] = this.value;   
    }); 
    o.push(t); 
    return o; 
}; 
+0

我的JS foo是可憐的,但這應該是一般的想法。有人請根據需要清理。 –

+0

樣本json是由我自己寫的,它並不完美。我想要的是對象數組(其中對象包含名字,姓氏和性別),以便我可以直接將其轉換爲java對象 – Tribhuwan

0
JSONObject myjson ; 
JSONArray the_json_array; 
StringBuilder builder = ... your jason content by buffer ..... 
      String a = "{child:"+builder.toString()+"}"; 
      myjson = new JSONObject(a); 
      the_json_array = myjson.getJSONArray("child"); 
      int size = the_json_array.length(); 
      ArrayList<JSONObject> arrays = new ArrayList<JSONObject>(); 
      for (int i = 0; i < size; i++) { 
       JSONObject another_json_object = the_json_array.getJSONObject(i); 
        arrays.add(another_json_object); 
      } 
     } catch (ClientProtocolException e) { 
      System.out.println("ClientProtocolException :"+e); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.out.println("IOException :"+e); 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      System.out.println("JSONException :"+e); 
      e.printStackTrace(); 
     } 
     return arrays; 
這個數據作爲 {[{"txtTitle":"Tribhuwan","txtName":"Dewangan","seGender":"Male"}, {"txtTitle":"Pankaj","txtName":"Sharma","seGender":"Male"}]} 感謝

我只是希望這個服務你,那種沒有青睞於我,我是通過httppost客戶端的響應有我sontent,存儲在構建器變量中。

相關問題