2016-04-20 13 views
1

Ich bin mit dem Problem in generischen Extrakt Objekt Fixture konfrontiert.Vergleich von Array von JSON-Objekten und JSON-Array

Meine Service-Schicht bietet mir Json so etwas. Es ist ein Array/Liste von Werten

[{"id":1,"description":"Anti-takeover Provision"}, 
{"id":2,"description":"Capital Structure"}, 
{"id":3,"description":"Director"}, 
{"id":4,"description":"Equity Plan"}, 
{"id":5,"description":"Executive Compensation"}, 
{"id":6,"description":"General Governance"}, 
{"id":7,"description":"Merger or Acquisition"}, 
{"id":12,"description":"Other"}, 
{"id":8,"description":"Proxy Contest"}, 
{"id":9,"description":"Reincorporation"}, 
{"id":10,"description":"Shareholder Proposal"}, 
{"id":11,"description":"Shareholder Rights"}] 

Aber meine Datenbank json

{"document":[{"VALUE":"Anti-takeover Provision","TOPIC_ID":1}, 
{"VALUE":"Capital Structure","TOPIC_ID":2}, 
{"VALUE":"Director","TOPIC_ID":3}, 
{"VALUE":"Equity Plan","TOPIC_ID":4}, 
{"VALUE":"Executive Compensation","TOPIC_ID":5}, 
{"VALUE":"General Governance","TOPIC_ID":6}, 
{"VALUE":"Merger or Acquisition","TOPIC_ID":7}, 
{"VALUE":"Other","TOPIC_ID":12}, 
{"VALUE":"Proxy Contest","TOPIC_ID":8}, 
{"VALUE":"Reincorporation","TOPIC_ID":9}, 
{"VALUE":"Shareholder Proposal","TOPIC_ID":10}, 
{"VALUE":"Shareholder Rights","TOPIC_ID":11}]} 

Wie kann ich diese beiden Werte leicht vergleichen?

+0

entsprechen Wie vergleiche ich diese beiden Werte? – Sujay66

Antwort

0
  1. Überprüfen Sie, ob die Längen sind die gleichen

  2. Sort the two lists durch ihre entsprechenden ID-Felder

  3. Überprüfen Sie, ob die Werte

    // stop as soon as you find a difference 
    
    for(i=0; i < listLength; i++) { 
    
        if (listA[i].id != listB[i].TOPIC_ID) { return false; } 
    
        if (listA[i].description != listB[i].VALUE) { return false; } 
    } 
    
    // if you get here, they must be the same 
    
    return true;