修复输入中的位置JSON

时间:2018-07-26 14:56:36

标签: javascript php jquery html json

我有这样的表单输入

<div class="new_form form-group"><input class="form-control" name="'+search_field+'['+isi+'][]" type="text" placeholder="Input new field '+isi+'"/></div>

<div class="new_form2 form-group"><select class="form-control select2" name="'+search_field+'['+isi+'][]" style="width: 100%;"><option value="TES">TESS</option></select></div>

并生成类似JSON的

{
"FirstName",
"TES",
}

如果我想要

这样的结果
{
"FirstName" : "TES"
}

我应该怎么做?

1 个答案:

答案 0 :(得分:0)

让我重申一下我认为您的目标是:

我有以下JSON:

{
  "_token": "eHHCMuIJ4XA7QaDDPywR1oTZIYxQV5WfWfBKzBz6",
  "resource_name": null,
  "method": "POST",
  "field1": { "key": "id", "value": "Object ID" },
  "field2": { "key": "Name", "value": {
    "array": [ "FirstName", "LastName", "ur first name", "ur last name" ]
  }}
}

我想将其转换为以下JSON:

{
  "_token": "eHHCMuIJ4XA7QaDDPywR1oTZIYxQV5WfWfBKzBz6",
  "resource_name": null,
  "method": "POST",
  "field1": { "key": "id", "value": "Object ID" },
  "field2": { "key": "Name", "value": {
    "array": { "FirstName": "ur first name", "LastName",  "ur last name" }
  }}
}

这是一种幼稚的(我不知道您的问题的更多细节)方法:

var myJSON = {
  "_token": "eHHCMuIJ4XA7QaDDPywR1oTZIYxQV5WfWfBKzBz6",
  "resource_name": null,
  "method": "POST",
  "field1": { "key": "id", "value": "Object ID" },
  "field2": { "key": "Name", "value": {
    "array": [ "FirstName", "LastName", "ur first name", "ur last name" ]
  }}
};

myJSON.field2.value.array = (function(jsArray){
  var jsObj = {};
  jsObj[jsArray[0]] = jsArray[2];
  jsObj[jsArray[1]] = jsArray[3];
  return jsObj;
})(myJSON.field2.value.array);

console.log(myJSON);