问题描述
我可以通过Google Apps脚本创建联系人,并添加电话号码,但是我无法添加地址,并收到错误消息“找不到您请求的资源。”
注意:fName,actualLastName,电子邮件,地址和phone1都是字符串
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;
import lombok.ToString;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JsonMongo2FormatsApp {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
JsonMapper mapper = JsonMapper.builder().build();
Response response = mapper.readValue(jsonFile,Response.class);
System.out.println(response.getErrorBody());
}
}
@Data
@ToString
class Response {
@JsonDeserialize(using = ErrorMapJsonDeserializer.class)
private Map<String,String> errorBody;
}
class ErrorMapJsonDeserializer extends JsonDeserializer<Map<String,String>> {
@Override
public Map<String,String> deserialize(JsonParser p,DeserializationContext ctxt) throws IOException {
TreeNode root = p.readValueAsTree();
if (!root.isObject()) {
// ignore everything except JSON Object
return Collections.emptyMap();
}
ObjectNode objectNode = (ObjectNode) root;
if (isOldFormat(objectNode)) {
return deserialize(objectNode);
}
return toMap(objectNode);
}
protected boolean isOldFormat(ObjectNode objectNode) {
final List<String> oldFormatKeys = Arrays.asList("_children","_nodeFactory","_class");
final Iterator<String> iterator = objectNode.fieldNames();
while (iterator.hasNext()) {
String field = iterator.next();
return oldFormatKeys.contains(field);
}
return false;
}
protected Map<String,String> deserialize(ObjectNode root) {
JsonNode children = root.get("_children");
Map<String,String> result = new LinkedHashMap<>();
children.fields().forEachRemaining(entry -> {
result.put(entry.getKey(),entry.getValue().get("_value").toString());
});
return result;
}
private Map<String,String> toMap(ObjectNode objectNode) {
Map<String,String> result = new LinkedHashMap<>();
objectNode.fields().forEachRemaining(entry -> {
result.put(entry.getKey(),entry.getValue().toString());
});
return result;
}
}
记录的错误消息是:“尝试添加地址时信息跌倒:找不到所需的资源。”
添加电话号码可以正常工作:
// create the Contact
var newContact = ContactsApp.createContact(fName,actualLastName,email);
var newName = newContact.getFullName();
Logger.log("newName: " + newName);
Logger.log("New contact added");
// attempt to add the address - DOESN'T WORK
try {
Logger.log("Wanting to add this address: ",address);
newContact.addAddress(ContactsApp.Field.WORK_ADDRESS,address);
Logger.log("Address added");
} catch(err) {
Logger.log("Stumbled while trying to add address: " + err.message);
browser.msgBox("Stumbled while trying to add address to contact");
}
联系人将被添加到适当的组:
newContact.addPhone(ContactsApp.Field.MOBILE_PHONE,phone1);
解决方法
作为一种解决方法,我想提出以下修改。在此修改中,创建联系人时将检索联系人ID。并且,addPhone
和addAddress
用于通过联系人ID检索联系人对象。
修改后的脚本:
var newContact = ContactsApp.createContact(fName,actualLastName,email);
var contactId = newContact.getId();
var c = ContactsApp.getContactById(contactId);
c.addAddress(ContactsApp.Field.WORK_ADDRESS,address);