Ajax und JSon
Prinzip:
- globale Variable im JavaScript-Abschnitt
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = readajax;
- Init-Methode, die von einem Event aufgerufen wird
- function testajax( form ) {..}
- Callback-Function, die vom XMLHttpRequest-Object aufgerufen wird
- status hat die Werte 0 bis 4
- status=4: Nachricht von Server ist angekommen
Code-Samples
Complete-samples
The NEW JSon-Object
If you want to use the nirmal JSon-Object, you must insert a jar-file. Then you use the put methods you create the JSon-String.
But this is not necessary. You better use my Json-Object-family new JSon-Object.
This methods used the reflection in java. With the reflection, one can print the name of the an object, all methods, all variables and you can get or set all values of the attribs. My Json-Object-family ist very powerful and very easy to use.
You need at least one method:
private String getJSon(Object obj)
This method create with one call the complete String!
If you have an Array or an ArrayList, you can use one og the following methods:
· private String getJSonArray(Object[] objs)
· private String getJSonArraylist(ArrayList objs)
These methods get all objects in the arrays and create the JSon-String:
[ {"name":"Miller", "mnr":12345},{"name":"Smith", "mnr":12346} ]
private String getJSon(Object obj) { StringBuilder sb = new StringBuilder(); try { Class c = obj.getClass(); // create a "class" from the object Field[] fields; fields = c.getDeclaredFields(); // get the attribs // create the JSon-String: { {"name": "Meier59", "mnr":12345} sb.append("{"); int n=fields.length-1; // need for the last comma for(int i=0; i<n; i++) { Field field = fields[i]; sb.append("\""); sb.append(field.getName()); sb.append("\": "); // if a String: the value has two 'double quotation marks' String s=field.getType().toString(); // language.String.... if (s.indexOf("String")>0) { sb.append("\""); sb.append(field.get(obj)); // get the value sb.append("\""); } else { sb.append(field.get(obj)); } if (i<n-1) { sb.append(", "); // get the value } } sb.append("}"); } catch ( IllegalAccessException e) { } return sb.toString(); }
createTable
This function create a html table with a JSon field!
function createTable(objs, numberDefinition, pindexnr, str_calledit, str_calldelete) {
let nl = '\n';
let obj = objs[0];
let captions = Object.keys(obj);
let s='';
s +='<table>' + nl;
s +='<tr>' + nl;
if (str_numberDefinition.length>0) {
s +=' <th>' + str_numberDefinition + '</th>' + nl;
}
for(var i in captions){
let scaption = captions[i];
let sfirst = captions[i].substring(0,1).toUpperCase();
s +=' <th>' + sfirst + scaption.substring(1,scaption.length) + '</th>' + nl;
}
if (str_calledit!='') {
s +=' <th>' + '-' + '</th>' + nl;
}
if (str_calldelete!='') {
s +=' <th>' + '-' + '</th>' + nl;
}
s +='</tr>' + nl;
for (let i in objs) {
s +='<tr>' + nl;
if (str_numberDefinition.length>0) {
s +=' <td>' + (i+1) + '</td>' + nl;
}
let obj = objs[i];
for(var j in obj){
//alert(j);
s +=' <td>' + obj[j] + '</td>' + nl;
}
if (str_calledit!='') {
s+=' <td class="tdbutton">' + '<input type="button" value="Edit" onclick="'+str_calledit+'(' + obj[pindexnr] + ')"/>' + '</td> ' + nl;
}
if (str_calldelete!='') {
s+=' <td class="tdbutton">' + '<input type="button" value="Delete" onclick="'+str_calldelete+'(' + obj[pindexnr] + ')"/>' + '</td> ' + nl;
}
s +='</tr>' + nl;
}
s +='</table>' + nl;
return s;
}
Literatur AJax und JSon
AJAX. schnell + kompakt Taschenbuch
von Christian Wenz
ISBN-10: 3935042922
ISBN-13: 978-3935042925
AJAX
von Klaus Zeppenfeld, Hassan El Moussaoui
Ajax: Grundlagen, Frameworks und Praxislösungen
von Stefan Mintert, Christoph Leisegang
Verlag: dpunkt;
ISBN-10: 3898644049
ISBN-13: 978-3898644044