2017-01-09 58 views
-1

[DUPICATE]如何通過JSON數組與Servlet

這我POST JSON代碼

var data_create = JSON.stringify($("#form_create_delegate").serializeArray()); 

     alert("isi data_create" + data_create); 

//  console.log(data_create) 
     $.ajax({ 
      type: "POST", 
      url: "formCreateDelegationServlet", 
      dataType: "json", 
      data:data:{JSONFile:data_create}, 
      success: function (response) { } 


     }); 

這是我的結果JSON

JSONFile:[{"name":"id_form_delegate","value":"1"},{"name":"merchant","value":"AMBHARA HOTEL TERBAIK"},{"name":"citty","value":"JAKARTA PUSAT"},{"name":"addrs","value":"JL. DIPONEGORO NO. 1J"},{"name":"province_merchant","value":"DKI JAKARTA"},{"name":"mid_merchant[]","value":"1"},{"name":"tid_merchant[]","value":"1"},{"name":"mid_merchant[]","value":"2"},{"name":"tid_merchant[]","value":"2"}] 

如何刪除屬性 「名」 和「價值」從我的JsonArray

我想製作一個這樣的格式

[{"merchant":"AMBHARA HOTEL TERBAIK","citty":"JAKARTA PUSAT"}] 

這是我的servlet代碼

JSONArray jsonArray = (JSONArray) net.sf.json.JSONSerializer.toJSON(request.getParameter("JSONFile")); 

      merchant = jsonObject.getString("merchant"); 
      city = jsonObject.getString("city"); 
      addrs = jsonObject.getString("addrs"); 
      province_merchant = jsonObject.getString("province_merchant"); 
      tid = jsonObject.getString("tid_merchant"); 
      mid = jsonObject.getString("tid_merchant"); 
//    
      System.out.println("nama_merchant: " + nama_merchant); 
      System.out.println("kota_merchant: " + kota_merchant); 
      System.out.println("alamat_merchant: " + alamat_merchant); 
      System.out.println("province_merchant: " + province_merchant); 
      System.out.println("mid: " + mid); 
      System.out.println("tid: " + tid); 

但它不如果你想擺脫鍵,只是保留值數組,你可以使用陣列工作

+3

,如果你刪除這兩個,你會得到空的對象。 –

+1

你是什麼意思刪除?如果你從對象中刪除這些屬性,將不會有任何問題 –

+0

你的問題並不清楚。也許你想要一個像這樣的數組的數組''[[「id_form_delegate」,「1」],[「merchant」,「」AMBHARA HOTEL TERBAIK「]] ...? – edc65

回答

1

。像這樣的地圖

var input=[{"name":"id_form_delegate","value":"1"},{"name":"merchant","value":"AMBHARA HOTEL TERBAIK"},{"name":"citty","value":"JAKARTA PUSAT"},{"name":"addrs","value":"JL. DIPONEGORO NO. 1J"},{"name":"province_merchant","value":"DKI JAKARTA"},{"name":"mid_merchant[]","value":"1"},{"name":"tid_merchant[]","value":"1"},{"name":"mid_merchant[]","value":"2"},{"name":"tid_merchant[]","value":"2"}] 
 

 
var result = input.map(function (obj) {return [obj.name, obj.value]}) 
 

 
console.log(JSON.stringify(result))

您可以使其成爲動態的,以便它可以使用任何屬性,而不僅僅是「名稱」和「值」,使用Object.keys(MSIE pre 9中不支持)。但要小心,你失去了對輸出值的順序的控制。

input=[{"name":"id_form_delegate","value":"1"},{"name":"merchant","value":"AMBHARA HOTEL TERBAIK"},{"name":"citty","value":"JAKARTA PUSAT"},{"name":"addrs","value":"JL. DIPONEGORO NO. 1J"},{"name":"province_merchant","value":"DKI JAKARTA"},{"name":"mid_merchant[]","value":"1"},{"name":"tid_merchant[]","value":"1"},{"name":"mid_merchant[]","value":"2"},{"name":"tid_merchant[]","value":"2"}] 
 

 
result = input.map(function (obj) { 
 
    return Object.keys(obj).map(function (key) {return obj[key]}) 
 
}) 
 

 
console.log(JSON.stringify(result))

+0

使之動態 –

+0

@AdamAzad這不是什麼OP在問,但這是一個有趣的任務 – edc65