2017년 12월 9일 토요일

json sample1

@RequestMapping(value = "ok", method = RequestMethod.POST)
public String ok(@Validated TestForm form)
{
System.out.print("");
String tttt = form.getTestList();

String a = tttt;
String saaaa = a.substring(1, a.length()-1 );

JSONParser jsonParser = new JSONParser();
         JSONObject jsonObj;
try {
jsonObj = (JSONObject) jsonParser.parse(saaaa);
JSONObject jsonObject = (JSONObject) jsonObj;

   for (Object key : jsonObject.keySet()) {
   String keyString = key.toString();
 

JSONArray memberArray = (JSONArray) jsonObject.get(keyString);

for(int i=0 ; i<memberArray.size() ; i++){
System.out.print("");
                JSONObject tempObj = (JSONObject) memberArray.get(i);
               
                JSONArray memberArray2 = (JSONArray) tempObj.get("workYearMonthListTable");
                for(int j=0; j<memberArray2.size(); j++)
                {
                JSONObject aaaa = (JSONObject) memberArray2.get(j);
                String temp1 = aaaa.get("name").toString();
                String temp2 = aaaa.get("id").toString();
                }
               
                JSONArray memberArray3 = (JSONArray) tempObj.get("machinListTable");
                for(int j=0; j<memberArray3.size(); j++)
                {
                JSONObject aaaa = (JSONObject) memberArray3.get(j);
                String temp1 = aaaa.get("name").toString();
                String temp2 = aaaa.get("id").toString();
                }
               
            }

    }


} catch (org.json.simple.parser.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       


return "next";
}



       
var bigTotal = [];        
var total = [];
// 리스트 생성
        //var testList = new Array();
var testList = []

for (var i=0; i < 5; i++)
{
//var employees  = [];
//employees["id"] = "100";
//employees["name"] = "Yashwant" + i;
//employees["age"] = "30";
  //employees.push({id:100, name:'Yashwant' + i, age:30});
var data = new Object();
data.id = i ;
            data.name = "Tester #" + i ;
           
            testList.push(data);
           
  //all.push(employees);
}

//var jsonEncode = JSON.stringify(testList);

var data2 = new Object();
data2.workYearMonthListTable = testList;
data2.machinListTable = testList;

total.push(data2);

//total.push({workYearMonthListTable: testList});
//total.push({machinListTable: testList});

var keyValue1 = 'site1';
var keyValue2 = 'site2';
//bigTotal[keyValue] = total;

var obj = {};

obj[keyValue1] = total;
obj[keyValue2] = total;

bigTotal.push(obj);

/*
var keyname = 'key';
var something = { };
something[keyname] = 'value';
*/

var jsonEncode = JSON.stringify(bigTotal);
alert(jsonEncode);

        var input = $("<input>").attr("type", "hidden").attr("name", "tableList").val(JSON.stringify(gridData));
        var input2 = $("<input>").attr("type", "hidden").attr("name", "testList").val(JSON.stringify(bigTotal));
  /*
        var inputObj = "";
        inputObj = document.createElement("input");
        $(inputObj).attr("type","hidden");
        $(inputObj).attr('name','tableList');
        $(inputObj).attr("value",JSON.stringify(tableList));
*/
        //$('#TestForm').append($(inputObj));
$('#dynamic_form').append($(input));
$('#dynamic_form2').append($(input2));
        //document.body.appendChild($('#TestForm'));
$("#TestForm").submit();

댓글 없음:

댓글 쓰기