简体   繁体   English

如何修改这个json对象

[英]How to modify this json object

How do i modify this code to get the intended outcome. 我如何修改此代码以获得预期的结果。

This is what i get from this code. 这就是我从这段代码中得到的。

{"name":"abc","identifier":"abc_one","number":"55","description":"This is a description"}

But i want a json object like this, 但是我想要一个这样的json对象,

{"project":  
  {"name":"abc",
   "identifier":"abc_one",
   "number":"55",
   "description":"This is a description"}
}

Please look at the code i have at the moment. 请查看我目前的代码。 It Need to be warped by a another object call "project". 它需要被另一个称为“项目”的对象扭曲。

 $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; $(function() { $('form').submit(function() { $('#result').text(JSON.stringify($('form').serializeObject())); return false; }); }); 
 <form action="" method="post"> First Name:<input type="text" name="name" maxlength="12" size="12"/> <br/> Last Name:<input type="text" name="identifier" maxlength="36" size="12"/> <br/> number:<input type="number" name="number" maxlength="36" size="12"/> <br/> <textarea wrap="physical" cols="20" name="description" rows="5">Enter your favorite quote!</textarea><br/> Select a Level of Education:<br/> <p><input type="submit" /></p> </form> 

All you need to change is following 您需要更改的只是以下内容

return o;

To

return {"project":o}

in serializeObject function. 在serializeObject函数中。

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

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