创建JSON字符串以在jQuery / Javascript中发布到web服务中

时间:2018-01-08 15:27:32

标签: javascript jquery json

我有一个html表单,一个按钮和一个单击处理程序来管理表单的提交。问题具体是关于通过AJAX提交表单的按钮的处理程序。

这是我写的处理程序:

JS:

 func cameraTracking() {
        motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
        motionManager.startDeviceMotionUpdates(to: OperationQueue.main) {
            [weak self](data: CMDeviceMotion?, error: Error?) in
            guard let data = data else { return }
            let attitude: CMAttitude = data.attitude

            self?.cameraNode.camera?.fieldOfView = 90

            self?.cameraNode.eulerAngles = SCNVector3Make(-Float(attitude.roll + Double.pi/2), Float(attitude.yaw), -Float(attitude.pitch) )

        }
    }

我使用 $('#create').click(function(event) { console.log('Create Button Click'); var dataJSON = JSON.stringify( $('#customer_form').serializeArray() ); console.log(dataJSON); $.ajax({ type: "POST", url: "/customer/", // The key needs to match your method's input parameter (case-sensitive). data: dataJSON, contentType: "application/json; charset=utf-8", dataType: "json", success: function(data){ console.log('Success'); console.log(data); }, failure: function(errMsg) { console.log('Error'); console.log(errMsg); } }); event.preventDefault(); }); 生成JSON字符串,得到结果 - 由JSON.stringify( $('#customer_form').serializeArray() );显示:

console.log(dataJSON)

这是获得以下结构的最简单方法:

[{"name":"name","value":"n"},{"name":"address_line_1","value":"a1"},{"name":"address_line_2","value":"a2"},{"name":"town","value":"t"},{"name":"postal_code","value":"p"},{"name":"region","value":"r"},{"name":"phone_1","value":"p"},{"name":"phone_2","value":"pa"},{"name":"fax","value":"f"},{"name":"email","value":"e"},{"name":"web","value":"w"}]

2 个答案:

答案 0 :(得分:2)

这样的事情应该有效。

final List<Integer> list = new ArrayList<>();
list.add(0);
final OctetString externalVlanIdsBitMapOctetString = BitMapUtils.createLeftBitmap(list);

答案 1 :(得分:0)

&#13;
&#13;
var arr = [{"name":"name","value":"n"},{"name":"address_line_1","value":"a1"},{"name":"address_line_2","value":"a2"},
{"name":"town","value":"t"},{"name":"postal_code","value":"p"},{"name":"region","value":"r"},{"name":"phone_1","value":"p"},{"name":"phone_2","value":"pa"},{"name":"fax","value":"f"},{"name":"email","value":"e"},{"name":"web","value":"w"}];

var acc = {};
//firstly lets flat your name and value to one new object
var newArr = arr.map(item=>{
                  var newItem={};
                  newItem[item.name] = item.value;
                  return newItem;
                });
     
//secondly we reduce flat all rows inside the newArr..
//..to one unique object acc   
newArr.forEach(item=>{
    Object.keys(item).forEach(key=> acc[key] = item[key]); 
});
 
//now you one unique object which you can serve..
// inside a new array [acc]
console.log([acc])
 

 
&#13;
&#13;
&#13;