簡體   English   中英

java中如何比較Json響應數據和sql數據

[英]How to compare the Json responedata and sql data in java

我在 webservice 中編寫了 Post 數據的代碼,它發送響應數據和代碼。 我使用選擇查詢在 sql 中發布的相同數據。 我的情況是我需要檢查 responsedata 和 sql 數據是否相同。

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=*****";  
      try {        
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         sqlConnection = DriverManager.getConnection(connectionUrl);
         String sqlStatement = "Enter the 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);
         }
      }
     catch (Exception e) {
         e.printStackTrace();
      }
     return 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 "); 
}

輸出 : 響應數據將存儲在 list.tostring() 與 formart

{
    "chargeback": 5,
    "activeDisputes": 12,
    "recentUpdates": 10,
    "recentlyClosed": 12,
    "responseRequired": 8,
    "inProgress": 4,
    "closedInFavor": 4,
    "closedChargebacks": 8
  }

我需要比較list.tostring值和 sql 值。 任何人都可以給出一個解決方案如何比較?

如果您有一個DisputeSummaryarraylistobject具有適當的 equals 和 hashcode 方法,那么您可以使用Gson將 json 數據直接解析為對象並進行比較。 例如。

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 you have an 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 you can iterate through the array and compare the objects with equals.
    }

}

示例爭議摘要對象

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() {
        return chargeback;
    }
    public void setChargeback(int chargeback) {
        this.chargeback = chargeback;
    }
    public int getActiveDisputes() {
        return activeDisputes;
    }
    public void setActiveDisputes(int activeDisputes) {
        this.activeDisputes = activeDisputes;
    }
    public int getRecentUpdates() {
        return recentUpdates;
    }
    public void setRecentUpdates(int recentUpdates) {
        this.recentUpdates = recentUpdates;
    }
    public int getRecentlyClosed() {
        return recentlyClosed;
    }
    public void setRecentlyClosed(int recentlyClosed) {
        this.recentlyClosed = recentlyClosed;
    }
    public int getResponseRequired() {
        return responseRequired;
    }
    public void setResponseRequired(int responseRequired) {
        this.responseRequired = responseRequired;
    }
    public int getInProgress() {
        return inProgress;
    }
    public void setInProgress(int inProgress) {
        this.inProgress = inProgress;
    }
    public int getClosedInFavor() {
        return closedInFavor;
    }
    public void setClosedInFavor(int closedInFavor) {
        this.closedInFavor = closedInFavor;
    }
    public int getClosedChargebacks() {
        return 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;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DisputeSummaryarraylistobject other = (DisputeSummaryarraylistobject) obj;
        if (activeDisputes != other.activeDisputes)
            return false;
        if (chargeback != other.chargeback)
            return false;
        if (closedChargebacks != other.closedChargebacks)
            return false;
        if (closedInFavor != other.closedInFavor)
            return false;
        if (inProgress != other.inProgress)
            return false;
        if (recentUpdates != other.recentUpdates)
            return false;
        if (recentlyClosed != other.recentlyClosed)
            return false;
        if (responseRequired != other.responseRequired)
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "DisputeSummaryarraylistobject [chargeback=" + chargeback
                + ", activeDisputes=" + activeDisputes + ", recentUpdates="
                + recentUpdates + ", recentlyClosed=" + recentlyClosed
                + ", responseRequired=" + responseRequired + ", inProgress="
                + inProgress + ", closedInFavor=" + closedInFavor
                + ", closedChargebacks=" + closedChargebacks + "]";
    }
}

除了檢查客戶端中的 JSON 文檔是否相同,您還可以直接在 SQL 中執行此操作:

select cast(? as jsonb) = (select jsonb_agg(jsonb_build_object(...)) from ...)

這對您來說可能不是正確的方法,而只是一個經常被忽視的想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM