简体   繁体   English

将 HTML 表单转换为 JSON 对象

[英]Convert HTML Form to JSON Object

I want to convert a HTML Form to a JSON object.我想将 HTML 表单转换为 JSON 对象。 The form basically contains two sections, Header and Detail.表单基本上包含两个部分,Header 和 Detail。 The Header section contains some HTML Input boxes and the Detail section is a Table. Header 部分包含一些 HTML 输入框,Detail 部分是一个表格。 A dummy view of my Form is as follows.我的表单的虚拟视图如下。

 <!DOCTYPE html> <html lang="en"> <body> <form id="MyForm" method="POST"> <div name="HeaderData"> <label id="lblFname">First Name:</label> <input type="text" name="fname"/> <br/> <label id="lblLname">Last Name:</label> <input type="text" name="lname" /> <br/> <label id="lblEmail">Email:</label> <input type="text" name="email" /> <br/> </div> <div id="DetailData"> <table cellspacing="0" align="Center" rules="all" border="1" id="MyTable" style="width:940px;border-collapse:collapse;"> </thead> <th scope="col">Code</th> <th scope="col">Name</th> <th scope="col">Continent</th> <th scope="col">Region</th> <th scope="col">Population</th> <th scope="col">Independence Year</th> </thead> <tbody> <tr> <td name="Code">Ind</td> <td name="Country">India</td> <td name="Continent">Asia</td> <td name="Region">Asia</td> <td name="Population">113Core</td> <td name="Independence">1947</td> </tr> <tr> <td name="Code">Ind</td> <td name="Country">India</td> <td name="Continent">Asia</td> <td name="Region">Asia</td> <td name="Population">1500000</td> <td name="Independence">1947</td> </tr> </tbody> </table> </div> </body> </form> </html>

I would expect the resulting JSON object to mimic:我希望生成的 JSON 对象能够模仿:

 {"HeaderData":[{"Fname":"XYZ","LName":"ABC","Email":"ABC@XYZ.COM"}], "DetailData":[{"Code":"Ind","Name":"India","Continent":"Asia","Region":"Asia","Population":"113 Crore","Independence Year":"1947"}], "DetailData":[{"Code":"Ind","Name":"India","Continent":"Asia","Region":"Asia","Population":"113 Crore","Independence Year":"1947"}] }

I have tried different libraries like jquery.tabletojson.js but they are not able to assign arrays to separate variables.我尝试过不同的库,如 jquery.tabletojson.js,但它们无法将数组分配给单独的变量。

Later I want to convert this JSON object to an XML String so that I can process it in a SQL Server QUERY.后来我想将此 JSON 对象转换为 XML 字符串,以便我可以在 SQL Server QUERY 中处理它。 I would expect the XML String to mimic:我希望 XML 字符串模仿:

 enter code here <Root> <HeaderData> <FName>XYZ</FName> <LName>XYZ</LName> <Email>abc@xyz.com</Email> </HeaderData> <DetailData> <Code>Ind</Code> <Name>India</Name> <Continent>Asia</Continent> <Region>Asia</Region> <Population>113Crore</Population> <IndependenceYear>1947</IndependenceYear> </DetailData> <DetailData> <Code>Ind</Code> <Name>India</Name> <Continent>Asia</Continent> <Region>Asia</Region> <Population>113Crore</Population> <IndependenceYear>1947</IndependenceYear> </DetailData> </Root>

My current priority is building the JSON STRING.我目前的首要任务是构建 JSON 字符串。 I can convert the data to XML once the JSON is available.一旦 JSON 可用,我就可以将数据转换为 XML。

if all your <td> 's have a name attribute you can convert it to an object with a few lines of jQuery...如果您所有的<td>都有一个 name 属性,您可以使用几行 jQuery 将其转换为对象...

var poop = [];
$("#MyTable").find("tr").each(function(){
    var fart = {};
    if(!$(this).find("td").length) return;
    $(this).find("td").each(function(){
        fart[$(this).attr("name")]=$(this).text();
    });
    poop.push(fart);
});

console.log(poop);

fiddle.小提琴。

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

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