javascript - request a json parameter to java to convert a json array to java array -
basicaly need to pass java string array javascipt array, after pass array via json jsp page, in jsp parse array java array.
i try following:
jsonarray arr = new jsonarray(); jsonobject tmp; for(int = 0; < invoiceid.length; i++) { tmp = new jsonobject(); tmp.put("invoiceid", invoiceid[i]); arr.add(tmp); }
following javascript in same page:
var invoiceid = json.stringify(<%=arr%>);` $.ajax({ type: 'get', url: 'crudsettlement.jsp', data: { winvoiceid: invoiceid }, async: false, datatype: 'json', success: function(json) { $("#msg").val(json.msg); } });
in page crudsettlement.jsp try following:
jsonobject jobj = new jsonobject(request.getparameter("mydata"));
but take folloing error incompatible types: string cannot converted map
any idea on how to convert json array java array? use json-simple.
do not know json , here rough logic
jsonarray arr = new jsonarray(request.getparameter("mydata")); list<string> jsonlist = new arraylist<string>(); (int = 0; < arr.length(); i++) { jsonlist.add(arr.getjsonobject(i).getstring("invoice")); } string[] invoices = jsonlist.toarray(new string[0]);
update , tried below seems work :
string json = " [{\"invoiceid\":\"0147708\"}]"; jsonarray array = (jsonarray) new jsonparser().parse(json); //jsonarray array = (jsonarray) jsonobject.get("invoiceid"); list<string> jsonlist = new arraylist<string>(); (int = 0; < array.size(); i++) { jsonlist.add(((jsonobject) array.get(i)).tostring()); } string[] invoices = jsonlist.toarray(new string[0]);
Comments
Post a Comment