简体   繁体   English

附加到JSON表示法?

[英]Appending to JSON notation?

I have the following JSON object: 我有以下JSON对象:

new Ajax.Request(url, {
   method: 'post',
   contentType: "application/x-www-form-urlencoded",
   parameters: {
          "javax.faces.ViewState": encodedViewState,
          "client-id": options._clientId,
          "component-value": options._componentValue
   }
});

Now, I would like to be able to append to the "Parameters" object programattically, but I am unsure of how I would actually do this. 现在,我希望能够以编程方式附加到“参数”对象,但我不确定我将如何实际执行此操作。

you can simply assign to it. 你可以简单地分配给它。 But you might want to do that before creating the request. 但您可能希望在创建请求之前执行此操作。

var parameters = {
      "javax.faces.ViewState": encodedViewState,
      "client-id": options._clientId,
      "component-value": options._componentValue
}
parameters.foo = 'bar';

var myAjax = new Ajax.Request(url, {
    method: 'post',
    contentType: "application/x-www-form-urlencoded",
    parameters: parameters
});

Assume that the JSON object is named as obj in the Ajax.Request Javascript function. 假设JSON对象在Ajax.Request Javascript函数中被命名为obj。 You could now add to the parameters object like this: 您现在可以添加到参数对象,如下所示:

obj['parameters']['someproperty'] = 'somevalue';

Hope this helps 希望这可以帮助

You can't append to it after you call new , because Prototype automatically sends and starts processing the Ajax request upon creation, instead do something like this if you need to alter the parameters object: 在调用new之后,您无法附加到它,因为Prototype会在创建时自动发送并开始处理Ajax请求,而是在需要更改参数对象时执行类似的操作:

var params = {
      "javax.faces.ViewState": encodedViewState,
      "client-id": options._clientId,
      "component-value": options._componentValue
      // Either add your additional properties here as:
      // propertyName : propertyValue
};
// Or add your properties here as:
// params.propertyName = propertyValue;
new Ajax.Request(url, {

method: 'post',

contentType: "application/x-www-form-urlencoded",

parameters: params
});

Here's a solution I used to append to the JSON object's existing data attribute, but I needed it to be generic enough to handle different key-value pairs. 这是我用来附加到JSON对象的现有data属性的解决方案,但我需要它足够通用以处理不同的键值对。 Here's what I created based on this post which seems to work for me. 这是我根据这篇文章创建的,似乎对我有用。

doAction : function(mData){
    this.data = mData;
    this.appendData = function(mDataToAppend){
        var jsonStrAr = JSON.stringify(mDataToAppend).replace('{','').replace('}','').split('","');
        for(var v = 0; v < jsonStrAr.length; v++){
            var m = jsonStrAr[v].split(':');
            this.data[m[0].replace(/"/g,'')] = m[1].replace(/"/g,'');
        }
    }
}

The result is a single JSON object with n to many attributes, which can then be sent over to the server via the JSON.stringify() command in the ajax request. 结果是一个具有n到多个属性的JSON对象,然后可以通过ajax请求中的JSON.stringify()命令将其发送到服务器。 Still getting comfortable/thinking with JSON, so there might be a better way to do this - in which case, I'm all ears. 仍然对JSON感到舒服/思考,所以可能有更好的方法来做到这一点 - 在这种情况下,我都是耳朵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM