フロントエンド:
1. JSONがリストオブジェクトから変換された場合、JSONを直接通過してデータを読むことができます。
2.フロントエンドリストオブジェクトをJSONに変換して背景に渡す必要があり、PARAMがAJAXのパラメーターである場合、変換は次のとおりです。
var jsonstr = json.stringify(list); var param = {}; param.jsonstr = jsonstr;背景:
1。文字列をリストに変換します(strをリストに変換)
リスト<t> list = new ArrayList <T>(); jsonarray jsonarray = jsonarray.fromobject(str); //文字列をjson list = jsonarray.tolist(jsonarray、t); //ここにクラス<t>
2。リストをJSONに変換します
jsonarray json = jsonarray.fromobject(object); string str = json.tostring(); // jsonを文字列に変換します
例えば:
1.ページユーザーが入力した情報に基づいて、回答オブジェクトのリストを形成します
/ ** * @param Answers * @param question_ids * @param types * @return */ private list <nass> toanswerlist(string [] dustentanswers、int [] question_ids、int [] ytes、int [] scores){list <nass> newnaylist = new ArrayList <回答>(); if(StudenAnswers!= null && question_ids!= null && types!= null && scores!= null){for(int i = 0; i <studentanswers.length; i ++){回答= new Assunt(); String StudentAnswer = dustentanswers [i]; int type = types [i]; int question_id = question_ids [i]; intスコア=スコア[i]; Answer.setQuestion_id(question_id); Answer.Setscore(スコア); Answer.SetStudenAnswer(StudenAnswer); Answer.SetType(タイプ); AnswerList.Add(Answer); }} return Answerlist; } / ** * json文字列をリストに変換 * @param props * @return * / public static list <nestion> convertanswerformstring(string answer){if(answer = null || answer.equals( ""))return new arrayList(); jsonarray jsonarray = jsonarray.fromobject(Answer); List <Answer> list =(list)jsonarray.tocollection(jsonarray、Answer.class);返品リスト。 } 2。クライアントページにユーザーが入力した情報に基づいて生成される回答オブジェクトのリストにJSON文字列を生成します。
public String getAnswerString(String [] StudentAnswers、int [] question_ids、int []タイプ、int []スコア){list list = toanswerlist(studenanswers、question_ids、ypes、scores); jsonarray jsonarray = jsonarray.fromobject(list); jsonarray.toString()を返します。 }PS:参照用のさらに実用的なJSONオンラインツールを次に示します。
オンラインJSONコード検証、検査、美化、フォーマットツール:
http://tools.vevb.com/code/json
JSONオンラインフォーマットツール:
http://tools.vevb.com/code/jsonformat
オンラインXML/JSON相互変換ツール:
http://tools.vevb.com/code/xmljson
JSONコードオンラインフォーマット/美化/圧縮/編集/変換ツール:
http://tools.vevb.com/code/jsoncodeformat
オンラインJSON圧縮/脱出ツール:
http://tools.vevb.com/code/json_yasuo_trans
C言語スタイル/HTML/CSS/JSONコードのフォーマットおよび美化ツール:
http://tools.vevb.com/code/ccode_html_css_json