[DUPICATE]
<击> 这个我的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中删除属性“name”和“value”
我想制作一个像这样的格式
[{"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);
但它不起作用
击>
答案 0 :(得分:1)
如果你想摆脱键并将值保存为数组,你可以像这样使用Array.map
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中不支持)使用任何属性而不仅仅是“name”和“value”。但请注意,您将失去对输出值的顺序的控制。
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))