2017-06-15 71 views
0

我正在開發一個Salesforce CRM項目,我需要訪問Google表格中的值,掃描某些關鍵字,並且如果它們符合特定條件,請複製給定行中的數據轉換爲Salesforce中的對象。解析來自Google表格的JSON響應

我使用Google Sheets API和Apex - Salesforce編程語言訪問Google表格的正文。 我遇到的問題是,我從Google表格文件獲取的每個數據行都是單獨的JSON文件。

正如您將在下面的示例中看到的,鍵僅位於第一個JSON文件中,那麼後面的每個文件都只包含值。

有沒有辦法將每個包含值(從第二個起)的JSON文件與第一個文件中的鍵配對?

這裏的JSON響應的樣子:

"range": "Angels!B2:AD2501", 
    "majorDimension": "ROWS", 
    "values": [ 
    [ 
     "Complete?", 
     "Name", 
     "ID :", 
     "Source", 
     "LinkedIn", 
     "Twitter", 
     "Profile", 
     "", 
     "AA Profile", 
     "Email", 
     "Location: City", 
     "Location: Country", 
     "Twitter Bio", 
     "Bio", 
     "Known For:", 
     "Investments", 
     "Preferred Industry", 
     "Vertical", 
     "Associated Venture Fund", 
     "Type", 
     "Total Investments", 
     "Total Exits", 
     "", 
     "Priority", 
     "Comments", 
     "Email", 
     "Contact Owner", 
     "Account Owner", 
     "In CRM" 
    ], 
    [ 
     "Yes", 
     "John Doe", 
     "2305", 
     "CrowdSourced", 
     "https://www.linkedin.com/in/someone-34738265", 
     "", 
     "", 
     "", 
     "https://angel.co/person", 
     "", 
     "Something", 
     "UK", 
     "", 
     "Executive Manager", 
     "Long term investor.", 
     "list, of, companies, separated,by, a, comma", 
     "IT, Advertising", 
     "", 
     "", 
     "Person (individual)", 
     "239", 
     "16", 
     "TRUE", 
     "H" 
    ], 
    [ 
     "Yes", 
     "A. Nikiforov", 
     "766", 
     "Pitchbook2", 
     "https://www.linkedin.com/pub/dir/alexey/nikiforov", 
     "", 
     "https://my.pitchbook.com?i=106763-86", 
     "", 
     "", 
     "[email protected]", 
     "Saint Petersburg", 
     "Russia", 
     "", 
     "Mr. A. Nikiforov is the Owner at Izdatelstvo Politekhnika. Mr. A. Nikiforov is the Owner at A. Nikiforov.", 
     " ", 
     "Izdatelstvo Politekhnika", 
     "Media", 
     "", 
     "", 
     "Angel (individual)", 
     "1", 
     "", 
     "FALSE" 
    ], 
    [ 
     "Yes", 
     "Aarish Patel", 
     "1043", 
     "Pitchbook2", 
     "https://www.linkedin.com/in/aarish-patel-06387983", 
     "", 
     "https://my.pitchbook.com?i=151254-01", 
     "", 
     "", 
     "", 
     "", 
     "", 
     "", 
     "Mr. Patel serves as the Non-Executive Director at Reds True Barbecue. He serves as the Angel Investor at Aarish Patel.", 
     " ", 
     "Reds True Barbecue", 
     "Restaurants, Hotels and Leisure, Retail", 
     "", 
     "", 
     "Angel (individual)", 
     "1", 
     "", 
     "FALSE" 
    ]]; 

回答

2

您可以通過數組的數組反序列化JSON的到頂點類,然後循環。結果是鍵列是列名的映射列表,值是相關值。

public class range { 
    List<List<String>> values; 
} 
public static List<Map<String,String>> parseJSON(String jsonStringFromCallout){ 
    range r = (range)JSON.deserialize(jsonStringFromCallout,range.class); 
    List<String> headers = range.values[0]; 
    List<Map<String,String>> allRows = new List<Map<String,String>>(); 
    for (Integer i=1; i<range.values.size(); i++){ 
    Map<String,String> thisRow = new Map<String,String>(); 
    for (Integer j=0; j<range.values[i].size(); j++){ 
     thisRow.put(headers[j],range.values[i][j]); 
    } 
    allRows.add(thisRow); 
    } 
    return allRows; 
} 
+0

謝謝馬特,上述工作,但現在我面臨一個堆大小太大的問題(這是一個包含2500行數據的電子表格)。 –

+0

這太糟糕了,要弄清楚的是JSONString本身是否太大,或者代碼的其他方面是否導致異常。要測試的內容是註釋代碼的一部分,並嘗試使用較小的JSON文件。 –

+0

嘿馬特,那就是我所做的。我將JSON分成更小的塊,現在它正在工作。感謝您對此的所有幫助! –