我得到了一个异常,感觉这可能是一个缺陷。我想知道其他人是否看到了这方面的问题。这是我正在编写的简化但可运行的服务。
import ballerina.net.http;
import ballerina.lang.messages;
import ballerina.lang.jsons;
import ballerina.lang.system;
@http:BasePath("/weather")
service WeatherService {
@http:GET
@http:Path("/current")
resource current(message m) {
string url = "http://api.mesowest.net/v2/stations/nearesttime?stid=KBFI&within=60&vars=air_temp,wind_speed,wind_direction&obtimezone=local&token=demotoken";
http:ClientConnector weatherConnector = create http:ClientConnector(url);
message request = {};
message jsonResponse = http:ClientConnector.get(weatherConnector, "", request);
json jsonDocument = messages:getJsonPayload(jsonResponse);
json timestamp;
string timeString;
try {
timestamp = jsons:getJson(jsonDocument, "$..PERIOD_OF_RECORD.end");
}
catch (exception e) {
system:println("Error getting timestamp");
}
messages:setJsonPayload(m, timestamp);
reply m;
}
}当我在调试器中运行这段代码时,从下面的json摘录中为JSON变量'timestamp‘分配了适当的值:
"STATION": [
{
"STATUS": "ACTIVE",
"MNET_ID": "1",
"PERIOD_OF_RECORD": {
"start": "1969-12-31T16:00:00-0800",
"end": "2017-02-27T19:40:00-0800"
}当我替换该行时:
timestamp = jsons:getJson(jsonDocument, "$..PERIOD_OF_RECORD.end"); 用这条线
timeString = jsons:getString(jsonDocument, "$..PERIOD_OF_RECORD.end");并停止和重新启动服务并对其进行测试时,它会在getString方法上抛出异常。我还没有找到打印异常或获取异常属性的方法,以便找出它失败的原因。控制台输出如下所示。
Running weather2.bal service.
ballerina: deploying service(s) in '/Users/clarkm2/Projects/Ballerina/Weather/weather2.bal'
ballerina: started server connector http-9090
Error getting timestamp对此有什么想法吗?如果这是一个缺陷,这些会在wso2.com JIRA站点上报告吗?
谢谢。
发布于 2017-02-28 13:51:59
出现此错误的原因是,$..PERIOD_OF_RECORD.end返回的元素不是字符串。它返回以下内容
"PERIOD_OF_RECORD": {
"end": "2017-02-27T21:00:00-0800",
"start": "1969-12-31T16:00:00-0800"
},它不能转换为文本。如果在catch块中记录异常,您将能够观察到以下错误。
Error getting timestamp : Failed to get string from json. Error while executing jsonpath: The element matching path: $..PERIOD_OF_RECORD.end is not a String.要记录异常,请将您的代码修改为:
catch (exception e) {
system:println("Error getting timestamp : " + exceptions:getMessage(e));
}with import ballerina.lang.exceptions导入语句。
发布于 2017-03-02 18:42:58
尝试按如下方式更改jsonpath:
string timeString = jsons:getString(j, "$.STATION[0].PERIOD_OF_RECORD.end");https://stackoverflow.com/questions/42500546
复制相似问题