解析Google表格中的JSON响应

时间:2017-06-15 22:30:57

标签: json api parsing salesforce apex

我正在开发一个Salesforce CRM项目,我需要访问Google表格中的值,扫描它们以查找某些关键字,如果它们符合特定条件,请将给定行中的数据复制到Salesforce中的对象中。

我使用Google表格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",
      "",
      "",
      "gfm@polytechnics.spb.ru",
      "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"
    ]];

1 个答案:

答案 0 :(得分:2)

您可以将json反序列化为Apex类,然后遍历数组数组。结果是一个映射列表,其中键是列名称,值是相关值。

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;
}