JSON to Export .txt Order in Java -
i have code, , string coming json server, , use these if statements prioritize want exported text file, when run it, output isnt output expecting, see below:
jsonobject attributeobject = objects.getjsonobject(objectattribute); string[] elementlist = jsonobject.getnames(attributeobject); (string attributename : elementlist) { if (attribute.equals("custodian")){ string value = objects.getstring("attributevalue"); system.out.print(value+","); out.write(value); out.append(","); } if (attribute.equals("custodian delegate")){ string value = objects.getstring("attributevalue"); system.out.print(value+","); out.write(value); out.append(","); } if (attribute.equals("authentication directory")){ string value = objects.getstring("attributevalue"); system.out.print(value+","); out.write(value); out.append(","); } if (attribute.equals("user id")){ string value = objects.getstring("attributevalue"); system.out.println(value); out.write(value); out.append(","); out.newline(); } }
expected output based if statements:
jdoe,cper,active directory, no
but once run it, output becomes:
active directory,jdoe,cper,no
is there easier way fix this? problem authentication directory goes first when start running program. tips? appreciate.
thanks in advance
make life easy creating model
class
class model{ string jdoe=""; //this example, change attribute names using naming convention string cper=""; string active_directory=""; string no=""; @override public string tostring(){ return jdoe+", " +cper+", " +active_directory+", " + no; } }
now change code use model, , update class model without writing file.
for example:
model model = new model(); ... for(whatever condition is){ //start of loop if (attribute.equals("custodian")){ string value = objects.getstring("attributevalue"); model.cper=value; //system.out.print(value+","); //out.write(value); <-- skip //out.append(","); <-- skip } ... } //after loop out.write(model.tostring());
note:
if have 2 loops place model model = new model();
inside outter loop
Comments
Post a Comment