Java read JSON array into list or object array

Source: Internet
Author: User

in previous development, it was often encountered to convert a list or array to JSON to the Web front end.

For front-end display, but today I encountered a need to pass the JSON array to the background, and after

The problem of converting the table into list. For this I spent a long time, so I wrote this blog.

The first is to construct the JSON array in the front end with JS, the HTML code is as follows:

<table style= "width:900px" id= "TABLE" ><pre name= "code" class= "HTML" > <tr id= "trhtml" ><td class= " Tdlabel "nowrap=" nowrap "><label><ju:txt value=" virtual account "/>:</label></td><td>< Input type= "text" id= "Payaccno" name= "Payaccno" vui-name= "Payaccno" style= "width:100px;" autocomplete= "Off"/>< /TD&GT;&LT;TD class= "Tdlabel" nowrap= "nowrap" ><label><ju:txt value= "account number"/>:</label></td ><td><input type= "text" id= "Glcode" name= "Glcode" vui-name= "Glcode" style= "width:100px;" autocomplete= " Off "/></td><td class=" Tdlabel "nowrap=" nowrap "><label><ju:txt value=" currency type "/><span Style= "color:red;" > *</span>:</label></td><td><select id= "ccy" name= "Ccy" class= "Ccy" style= "width:80px  ; text-align:center; " Vui-name= "Ccy" vui-validate= "Required:true" > </select><td/><td class= "Tdlabel" nowrap= "nowrap" ><label><ju:txt value= "Amount"/&Gt;<span style= "color:red;" > *</span>:</label></td><td><input type= "text" id= "Amount" name= "Amount" style= "width  : 80px; " Vui-name= "Amount" vui-validate= "Required:true,type:number"/> <td/><td class= "Tdlabel" nowrap= "nowrap" ><label><ju:txt value= "Summary"/><span style= "color:red;"  > *</span>:</label></td><td><textarea id= "Remark" name= "remark" style= "width:200px;" Vui-name= "Remark" vui-validate= "required:true,maxlen:20" > </textarea><td/><td style= " padding-left:35px; " ><td/><td style= "Padding-left : 15px; " ><td/> </tr></TABLE>
in the requirements of the need to dynamically add and delete content in the TR to add a line of input, for this there is the following JS code:
Bindings add addhtml= "<tr>" +$ ("#trHtml"). HTML () + "</tr>"; $ ("#handworkAccountForm"). Find ($ ("#create"). (function () {$ ("#create"). Die ("click"); $ ("#create"). Live ("Click", Function () {$ ("#table"). Append (addhtml); $ (". Ccy : Last "). html (modelshtml);   }); ////Bind Delete $ ("#handworkAccountForm"). Find ($ ("#delete")). each (function () {$ ("#delete"). Live ("Click", Function () {if ($ ( "#table tr"). Length==1) {return}$ (this). Parent (). Parent (). remove ();});});
where addhtml is the option item in select and is the load from the background with Ajax, ignored here.

Second, you need to use JS to get the input of a TR term, constructed into a JSON array, the code is as follows:

var savehandworkaccount = function () {var jsondata={"ResourceID": "2020004"};var handworkaccountlist=new Array;var Jsonstr= ""; $ ("#table tr TD Input,select,textarea"). each (function (i) {var name=$ (this). attr ("name"), Var value=$ (this) . val (); jsonstr=jsonstr+ "\" "+name+" \ ":" + "\" "+value+" \ "+", "if (i%4==0&i!=0) {jsonstr=" {"+jsonstr.substring (0 , jsonstr.length-1) + "}", Var A=jquery.parsejson (JSONSTR); Handworkaccountlist.push (a); Jsonstr= "";}}); jsondata["Handworkaccountlist"]=json.stringify (handworkaccountlist); $.vajax ({url: "Handworkaccountprocess.json" , Data:jsondata,datatype: "JSON", success:function (data) {if (data.errormsg) {alert (data.errormsg); return;} if (data.info) {alert (data.info); Location.reload ();} Else{alert ("Operation failed");}},error:function (JQXHR, Textstatus, error) {alert ("Load page failed! ");}});};
The JSON format is constructed as follows:
{"    handworkaccountlist": [        {            "payaccno": "122",            "Glcode": "333",            "Ccy": "CNY",            "Amount": " "Remark"            : "5555"        },        {            "payaccno": "5555", "Glcode": "            6666",            "Ccy": "CNY",            " Amount ":" 888 "        }    ],    " ResourceID ": 2020004}
Finally, the background gets the code as follows:
public void        Addhandworkaccount (Context context) {Logger.debug ("Start addhandworkaccount ...");        map<string, object> request = This.getrequestdata ();        map<string, object> response = This.getresponsedata ();        Object handworkaccountlist = Request.get ("Handworkaccountlist");        String json = handworkaccountlist.tostring ();        Jsonarray array = jsonarray.fromobject (JSON);        List

Where list

Is the data transfer object used to encapsulate the parameters passed by the front end, with the following code:

public class Handworkaccountdto implements Serializable {    protected String payaccno;    protected String Glcode;    protected String ccy;    protected BigDecimal amount;    protected String remark;    //.... Omit Get,set Method}

at this point, Java reads the case of a JSON array into a list or an object array.





Java read JSON array into list or object array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.