JSONArray responses = resp.getJSONArray("resources");
LOG.debug("Found contacts : " + responses.length());
for (int i = 0; i < responses.length(); i++) {
JSONObject obj = responses.getJSONObject(i);
JSONObject fields = obj.getJSONObject("fields");
Contact p = new Contact();
if (obj.has("record_type")) {
if ("company".equals(obj.getString("record_type"))) {
if (fields.has("company name")) {
JSONArray arr = fields.getJSONArray("company name");
JSONObject jobj = arr.getJSONObject(0);
if (jobj.has("value")) {
p.setFirstName(jobj.getString("value"));
}
}
if (obj.has("avatar_url")) {
p.setProfileImageURL(obj.getString("avatar_url"));
}
if (fields.has("URL")) {
JSONArray arr = fields.getJSONArray("URL");
JSONObject jobj = arr.getJSONObject(0);
if (jobj.has("value")) {
p.setProfileUrl(jobj.getString("value"));
}
}
plist.add(p);
} else if ("person".equals(obj.getString("record_type"))) {
if (fields.has("last name")) {
JSONArray arr = fields.getJSONArray("last name");
JSONObject jobj = arr.getJSONObject(0);
if (jobj.has("value")) {
p.setLastName(jobj.getString("value"));
}
}
if (fields.has("first name")) {
JSONArray arr = fields.getJSONArray("first name");
JSONObject jobj = arr.getJSONObject(0);
if (jobj.has("value")) {
p.setFirstName(jobj.getString("value"));
}
}
if (obj.has("avatar_url")) {
p.setProfileImageURL(obj.getString("avatar_url"));
}
if (fields.has("URL")) {
JSONArray arr = fields.getJSONArray("URL");
if (arr.length() == 1) {
JSONObject jobj = arr.getJSONObject(0);
if (jobj.has("value")) {
p.setProfileUrl(jobj.getString("value"));
}
} else {
String url = null;
for (int k = 0; k < arr.length(); k++) {
JSONObject jobj = arr.getJSONObject(k);
url = jobj.optString("value");
if ("personal".equals(jobj
.optString("modifier"))) {
break;
}
}
if (url != null) {
p.setProfileUrl(url);
}
}
}
plist.add(p);
}