How to compare the Json responedata and sql data in java -



How to compare the Json responedata and sql data in java -

i written code post info in webservice , sends response info , code. same info posting in sql using select query. scenario need check responsedata , sql info same or not.

mysql.java

public arraylist<disputesummaryarraylistobject> connectsqlconnectiondisputesummary() throws sqlexception, classnotfoundexception{ arraylist<disputesummaryarraylistobject> disputesummaryarraylistobjectlist = new arraylist<disputesummaryarraylistobject>(); string connectionurl = "jdbc:sqlserver://localhost:1433;" + "databasename=adventureworks;user=username;password=*****"; seek { class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver"); sqlconnection = drivermanager.getconnection(connectionurl); string sqlstatement = "enter sql statement"; statement = sqlconnection.createstatement(); resultset = statement.executequery(sqlstatement); disputesummaryarraylistobject disputesummaryarraylistobject = new disputesummaryarraylistobject(); while (resultset.next()) { disputesummaryarraylistobject.setchargeback(resultset.getint("chargeback")); disputesummaryarraylistobject.setactivedisputes(resultset.getint("activedisputes")); disputesummaryarraylistobject.setrecentupdates(resultset.getint("recentupdates")); disputesummaryarraylistobject.setrecentlyclosed(resultset.getint("recentlyclosed")); disputesummaryarraylistobject.setresponserequired(resultset.getint("responserequired")); disputesummaryarraylistobject.inprogress(resultset.getint("inprogress")); disputesummaryarraylistobject.setclosedinfavor(resultset.getint("closedinfavor")); disputesummaryarraylistobject.setclosedchargebacks(resultset.getint("closedchargebacks")); disputesummaryarraylistobjectlist.add(disputesummaryarraylistobject); } } grab (exception e) { e.printstacktrace(); } homecoming disputesummaryarraylistobjectlist; }

}

myjsonresponse.java

public void convertresponseoutputtojsonobject() throws ioexception, parseexception { logger.info("convertresponseoutputtojsonobject() : begins "); jsonparser jsonparser = new jsonparser(); object object = jsonparser.parse(response.tostring()); jsonarray jsarray = (jsonarray) object; iterator<jsonobject> iterator = jsarray.iterator(); while (iterator.hasnext()) { jsonobject jsonobj = iterator.next(); list.add(jsonobj); } logger.info(list.tostring()); logger.info("convertresponseoutputtojsonobject() : ends "); }

output : response info stored in list.tostring() formart

{ "chargeback": 5, "activedisputes": 12, "recentupdates": 10, "recentlyclosed": 12, "responserequired": 8, "inprogress": 4, "closedinfavor": 4, "closedchargebacks": 8 }

i need compare list.tostring values , sql values. can give solution how compare?

if have disputesummaryarraylistobject proper equals , hashcode method can utilize gson parse json info straight object , comparision. eg.

import java.lang.reflect.type; import java.util.list; import com.google.gson.gson; import com.google.gson.reflect.typetoken; public class jsoncompare { public static void main(string[] args) { string jsondata = "{ \"chargeback\": 5, \"activedisputes\": 12, \"recentupdates\": 10, \"recentlyclosed\": 12, \"responserequired\": 8, \"inprogress\": 4, \"closedinfavor\": 4, \"closedchargebacks\": 8 }"; gson gson = new gson(); disputesummaryarraylistobject webserviceobject = gson.fromjson(jsondata, disputesummaryarraylistobject.class); //ddobject disputesummaryarraylistobject dbobject = new disputesummaryarraylistobject(); dbobject.setchargeback(5); dbobject.setactivedisputes(12); dbobject.setrecentupdates(10); dbobject.setrecentlyclosed(12); dbobject.setresponserequired(8); dbobject.setinprogress(4); dbobject.setclosedinfavor(4); dbobject.setclosedchargebacks(8); system.out.println(webserviceobject.equals(dbobject)); // if have array of objects string jsondataarray = "[ { \"chargeback\": 5, \"activedisputes\": 12, \"recentupdates\": 10, \"recentlyclosed\": 12, \"responserequired\": 8, \"inprogress\": 4, \"closedinfavor\": 4, \"closedchargebacks\": 8 }, " + " { \"chargeback\": 6, \"activedisputes\": 7, \"recentupdates\": 8, \"recentlyclosed\": 2, \"responserequired\": 5, \"inprogress\": 14, \"closedinfavor\": 14, \"closedchargebacks\": 5 } ]"; type listtype = new typetoken<list<disputesummaryarraylistobject>>() {}.gettype(); list<disputesummaryarraylistobject> disputesummaryarraylistobjectlist = gson.fromjson(jsondataarray, listtype); system.out.println(disputesummaryarraylistobjectlist); //now can iterate through array , compare objects equals. } }

a sample dispute summary object

public class disputesummaryarraylistobject { private int chargeback; private int activedisputes; private int recentupdates; private int recentlyclosed; private int responserequired; private int inprogress; private int closedinfavor; private int closedchargebacks; public int getchargeback() { homecoming chargeback; } public void setchargeback(int chargeback) { this.chargeback = chargeback; } public int getactivedisputes() { homecoming activedisputes; } public void setactivedisputes(int activedisputes) { this.activedisputes = activedisputes; } public int getrecentupdates() { homecoming recentupdates; } public void setrecentupdates(int recentupdates) { this.recentupdates = recentupdates; } public int getrecentlyclosed() { homecoming recentlyclosed; } public void setrecentlyclosed(int recentlyclosed) { this.recentlyclosed = recentlyclosed; } public int getresponserequired() { homecoming responserequired; } public void setresponserequired(int responserequired) { this.responserequired = responserequired; } public int getinprogress() { homecoming inprogress; } public void setinprogress(int inprogress) { this.inprogress = inprogress; } public int getclosedinfavor() { homecoming closedinfavor; } public void setclosedinfavor(int closedinfavor) { this.closedinfavor = closedinfavor; } public int getclosedchargebacks() { homecoming closedchargebacks; } public void setclosedchargebacks(int closedchargebacks) { this.closedchargebacks = closedchargebacks; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + activedisputes; result = prime * result + chargeback; result = prime * result + closedchargebacks; result = prime * result + closedinfavor; result = prime * result + inprogress; result = prime * result + recentupdates; result = prime * result + recentlyclosed; result = prime * result + responserequired; homecoming result; } @override public boolean equals(object obj) { if (this == obj) homecoming true; if (obj == null) homecoming false; if (getclass() != obj.getclass()) homecoming false; disputesummaryarraylistobject other = (disputesummaryarraylistobject) obj; if (activedisputes != other.activedisputes) homecoming false; if (chargeback != other.chargeback) homecoming false; if (closedchargebacks != other.closedchargebacks) homecoming false; if (closedinfavor != other.closedinfavor) homecoming false; if (inprogress != other.inprogress) homecoming false; if (recentupdates != other.recentupdates) homecoming false; if (recentlyclosed != other.recentlyclosed) homecoming false; if (responserequired != other.responserequired) homecoming false; homecoming true; } @override public string tostring() { homecoming "disputesummaryarraylistobject [chargeback=" + chargeback + ", activedisputes=" + activedisputes + ", recentupdates=" + recentupdates + ", recentlyclosed=" + recentlyclosed + ", responserequired=" + responserequired + ", inprogress=" + inprogress + ", closedinfavor=" + closedinfavor + ", closedchargebacks=" + closedchargebacks + "]"; } }

java sql json

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -