I wrote jsp functions to list all file names and all sub-folder names in a directory,and I want to save the result in json object.In the first step, I recursively read the all file names and all sub-folder names,and I saved the results in a string:
res_data ="my_report3.rptdesign&my_report4.rptdesign&28.12.2015$my_report.rptdesign&my_report1.rptdesign&my_report2.rptdesign&30.12.2015$customerst.rptdesign&TopNPercent.rptdesign&22.12.2015$by_sup_ML.rptdesign&chartcwong.rptdesign&HTML5 Chart.rptdesign&23.12.2015$main_page.rptdesign&my_report.rptdesign&my_report18.rptdesign&my_report19.rptdesign&my_report20.rptdesign&my_report21.rptdesign&my_report22.rptdesign&my_report23.rptdesign&my_report24.rptdesign&postgreSQL.rptdesign&test.rptdesign&TopNPercent.rptdesign&PathFind_Report"
In the second step, I wrote the following code to write the result data into json oject:
JSONObject json_obj=new JSONObject(); JSONArray obj_array = new JSONArray(); String[] first_res_d = res_data.split("[$]"); for( int i = 0 ; i < first_res_d.length ; i ++ ) { String[] second_res_d = first_res_d[i].split("[&]"); int id_folder = second_res_d.length-1; for( int j = 0 ; j < id_f ; j ++ ) { json_obj.put("parent",second_res_d[id_folder]);
json_obj.put("child",second_res_d[j]); obj_array.add( json_obj); } } out.println(obj_array);
But, I only got the repeating of only one same json object. The printed result is as follows:
[{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"},{"parent":"PathFind_Report","child":"TopNPercent.rptdesign"}]
Could you give me some suggestions to correct my code ,please ? thanks a lot in advance .