Json2apex-消息消耗无法识别的属性

时间:2019-02-08 12:58:36

标签: salesforce apex apex-code jsonparser

我无法获取“消息和文本字段”节点的所有详细信息。 获取错误为: 邮件消耗无法识别的属性:名称, 邮件消耗无法识别的属性:userId, 邮件消耗无法识别的属性:项目, 邮件消耗无法识别的属性:文本, 消耗消息的无法识别的属性:scratchPeekEnabled,

这是我的json:

{"messages":[{"type":"ChatEstablished", "message":{"name":"Customer Support","userId":"0056A000000ivth","items":[],"sneakPeekEnabled":true,"chasitorIdleTimeout":{"isEnabled":false}}},{"type":"ChatMessage", "message":{"text":"Welcome to Wyndham chat service. How can I help you?","name":"Customer Support","schedule":{"responseDelayMilliseconds":0.0},"agentId":"0056A000000ivth"}}], "sequence":2, "offset":1401052138}'

我通过https://json2apex.herokuapp.com创建了一个顶点类。 这是顶点类的代码。

public class LiveAgentJsonParser {

public class Messages {
    public String type_Z {get;set;} // in json: type
    public Message message {get;set;} 

    public Messages(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'type') {
                        type_Z = parser.getText();
                    } else if (text == 'message') {
                        message = new Message(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Messages consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Message {
    public List<CustomDetails> customDetails {get;set;} 
    public Integer connectionTimeout {get;set;} 
    public String visitorId {get;set;} 
    public List<CustomDetails> sensitiveDataRules {get;set;} 
    public Boolean transcriptSaveEnabled {get;set;} 
    public String url {get;set;} 
    public Integer queuePosition {get;set;} 
    public GeoLocation geoLocation {get;set;} 

    public Message(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'customDetails') {
                        customDetails = arrayOfCustomDetails(parser);
                    } else if (text == 'connectionTimeout') {
                        connectionTimeout = parser.getIntegerValue();
                    } else if (text == 'visitorId') {
                        visitorId = parser.getText();
                    } else if (text == 'sensitiveDataRules') {
                        sensitiveDataRules = arrayOfCustomDetails(parser);
                    } else if (text == 'transcriptSaveEnabled') {
                        transcriptSaveEnabled = parser.getBooleanValue();
                    } else if (text == 'url') {
                        url = parser.getText();
                    } else if (text == 'queuePosition') {
                        queuePosition = parser.getIntegerValue();
                    } else if (text == 'geoLocation') {
                        geoLocation = new GeoLocation(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Message consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public List<Messages> messages {get;set;} 
public Integer sequence {get;set;} 

public LiveAgentJsonParser(JSONParser parser) {
    while (parser.nextToken() != System.JSONToken.END_OBJECT) {
        if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
            String text = parser.getText();
            if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                if (text == 'messages') {
                    messages = arrayOfMessages(parser);
                } else if (text == 'sequence') {
                    sequence = parser.getIntegerValue();
                } else {
                    System.debug(LoggingLevel.WARN, 'LiveAgentJsonParser consuming unrecognized property: '+text);
                    consumeObject(parser);
                }
            }
        }
    }
}

public class GeoLocation {
    public String organization {get;set;} 
    public String region {get;set;} 
    public String city {get;set;} 
    public String countryName {get;set;} 
    public Double latitude {get;set;} 
    public String countryCode {get;set;} 
    public Double longitude {get;set;} 

    public GeoLocation(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'organization') {
                        organization = parser.getText();
                    } else if (text == 'region') {
                        region = parser.getText();
                    } else if (text == 'city') {
                        city = parser.getText();
                    } else if (text == 'countryName') {
                        countryName = parser.getText();
                    } else if (text == 'latitude') {
                        latitude = parser.getDoubleValue();
                    } else if (text == 'countryCode') {
                        countryCode = parser.getText();
                    } else if (text == 'longitude') {
                        longitude = parser.getDoubleValue();
                    } else {
                        System.debug(LoggingLevel.WARN, 'GeoLocation consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class CustomDetails {

    public CustomDetails(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    {
                        System.debug(LoggingLevel.WARN, 'CustomDetails consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}


public static LiveAgentJsonParser parse(String json) {
    System.JSONParser parser = System.JSON.createParser(json);
    return new LiveAgentJsonParser(parser);
}

public static void consumeObject(System.JSONParser parser) {
    Integer depth = 0;
    do {
        System.JSONToken curr = parser.getCurrentToken();
        if (curr == System.JSONToken.START_OBJECT || 
            curr == System.JSONToken.START_ARRAY) {
            depth++;
        } else if (curr == System.JSONToken.END_OBJECT ||
            curr == System.JSONToken.END_ARRAY) {
            depth--;
        }
    } while (depth > 0 && parser.nextToken() != null);
}



private static List<Messages> arrayOfMessages(System.JSONParser p) {
    List<Messages> res = new List<Messages>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new Messages(p));
    }
    return res;
}








private static List<CustomDetails> arrayOfCustomDetails(System.JSONParser p) {
    List<CustomDetails> res = new List<CustomDetails>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new CustomDetails(p));
    }
    return res;
}

}

预先感谢

0 个答案:

没有答案