简体   繁体   English

如何将表单字段转换为Json Object?

[英]how to convert form fields to Json Object?

For example the form contains fields with the following names txt01, txt02, txt03[], txt03[], txt04[name][], txt04[name][], txt04[phone][], txt04[phone][], txt05[][name], txt05[][phone], txt05[][name], txt05[][phone]. 例如,表单包含具有以下名称的字段txt01,txt02,txt03 [],txt03 [],txt04 [名称] [],txt04 [名称] [],txt04 [电话] [],txt04 [电话] [], txt05 [] [名称],txt05 [] [电话],txt05 [] [名称],txt05 [] [电话]。 When I input values to those fields and click the submit button it should generate the below Json Object: 当我在这些字段中输入值并单击提交按钮时,它应生成以下Json对象:

Object {
    txt01: "Text 01",
    txt02: "Text 02",
    txt03: Array(2) {
        0: "Text 01",
        1: "Text 02"
    },
    txt04: Object 
    {
        name: Array()
        {
            0: Text 01
            1: Text 02
        },

        phone: Array()
        {
            0: 000001
            1: 000002
        }
    },
    txt05: Array(2) 
    {
        0: Object
        {
            name: Text 01
            phone: 000001
        },

        1: Object
        {
            name: Text 02
            phone: 000002
        }
    }

}

Below is the form that is to be serialize to Json Object. 下面是要序列化为Json Object的形式。 The script that will perform the serialization should generate the above Json Object. 将执行序列化的脚本应生成上面的Json对象。

<form>
    <input type="text" name="txt01" value="Text 01" />
    <input type="text" name="txt02" value="Text 02" />

    <input type="text" name="txt03[]" value="Text 01" />
    <input type="text" name="txt03[]" value="Text 02" />

    <input type="text" name="txt04[name][]" value="Text 01" />
    <input type="text" name="txt04[name][]" value="Text 02" />
    <input type="text" name="txt04[phone][]" value="000001" />
    <input type="text" name="txt04[phone][]" value="000002" />

    <input type="text" name="txt05[][name]" value="Text 01" />
    <input type="text" name="txt05[][phone]" value="000001" />
    <input type="text" name="txt05[][name]" value="Text 02" />
    <input type="text" name="txt05[][phone]" value="000002" />

    <input type="submit" value="Submit" />
</form>

I was doing some research on it here yet I didn't find a respond that fits on what I am doing so I created a solution. 我在这里进行了一些研究,但没有找到适合我正在做的回应,因此我创建了一个解决方案。 For more details you can visit it here https://github.com/citnvillareal/serializeObject 有关更多详细信息,您可以在这里访问它https://github.com/citnvillareal/serializeObject

Please don't hesitate to contribute. 请不要犹豫,贡献自己的力量。 I hope this can help. 希望对您有所帮助。

Just want to share my ideas on this. 只想分享我的想法。 I just develop an application form to json using native javascript. 我只是使用本机javascript将应用程序表单开发为json。 You may visit this link: https://github.com/jhanxtreme/form-to-json 您可以访问此链接: https : //github.com/jhanxtreme/form-to-json

EXAMPLE FORM 示例表格

<form name="f1" onsubmit="return formToJson(this);">
   <input type="text" name="username" value="dummyuser" / >
   <input type="password" name="password" value="password123" />
   <input type="submit" />
</form>

JSON OUTPUT: JSON输出:

{
 username:"dummyuser",
 password:"password123"
}

Also this will map most of the form elements including HTML5 into JSON data. 同样,这会将包括HTML5在内的大多数表单元素映射到JSON数据中。 Hope this helps you. 希望这对您有所帮助。

$.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;
};

This is from Convert form data to JavaScript object with jQuery 这是从使用jQuery将表单数据转换为JavaScript对象

You might try looking around a bit for something that sounds like a lot of people could use :) 您可能会尝试环顾四周,听起来似乎很多人都可以使用:)

still not clear the requirement but i guess you can do something like that 仍然没有明确要求,但我想你可以做这样的事情

 var frm = $('form');
 var data = JSON.stringify(frm.serializeArray());
var myObj = new Object();
myObj.text01 = "Text 01";
myObj.text02 = "Text 02";
.
.
.
var string =JSON.stringify(myObj);

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

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