gpt4 book ai didi

apama - 如何解析 HTTP JSON 响应负载中的值

转载 作者:行者123 更新时间:2023-12-02 20:08:26 26 4
gpt4 key购买 nike

我对 Apama 还比较陌生。我使用的是 v10.3.1。我按照以下代码片段在监视器中执行 REST 请求:

http://www.apamacommunity.com/documents/10.3.1.1/apama_10.3.1.1_webhelp/apama-webhelp/#page/apama-webhelp%2Fco-ConApaAppToExtCom_httpclient_using_predefined_generic_event_definitions.html%23wwconnect_header

当前处理响应的操作如下所示:


action handleResponse(Response response){
if response.isSuccess(){
print "###The response payload is :" + response.payload.toString();
}

else {
print "###Request failed. Response status is: " + response.statusCode.toString() + " | " + response.statusMessage;
}
}

我正在寻找在 JSON 响应负载中提取以下属性值的最佳方法:

assetparents.references[0].managedObject.name (here “SOME”).

我尝试了不同的方法,但总是遇到错误。

打印语句为响应负载提供以下输出:



###The response payload is :com.apama.util.AnyExtractor(any(string,"

{"owner":"some@one.com","additionParents":{
"self":"https://somebaseurl/inventory/managedObjects/5706999/additionParents","references":[]
},
"childDevices":{
"self":"https://somebaseurl/inventory/managedObjects/5706999/childDevices","references":[]
},

"childAssets":{
"self":"https://somebaseurl/inventory/managedObjects/5706999/childAssets","references":[]
},
"creationTime":"2019-05-09T11:36:10.197Z",
"lastUpdated":"2019-05-10T05:28:07.893Z",
"childAdditions":{
"self":"https://somebaseurl/inventory/managedObjects/5706999/childAdditions",
"references":[{
"managedObject":{"name":"Escalate alarmtest",
"self":"https://somebaseurl/inventory/managedObjects/5706698",
"id":"5706698"},
"self":"https://somebaseurl/inventory/managedObjects/5706999/childAdditions/5706698"
}
]},
"name":"SOME Test Device",
"deviceParents":{
"self":"https://somebaseurl/inventory/managedObjects/5706999/deviceParents",
"references":[]
},
"assetParents":{
"self":"https://somebaseurl/inventory/managedObjects/5706999/assetParents",
"references":[{
"managedObject":{
"additionParents":{
"self":"https://somebaseurl/inventory/managedObjects/5706682/additionParents",
"references":[]
},
"childDevices":{
"self":"https://somebaseurl/inventory/managedObjects/5706682/childDevices",
"references":[]
},
"childAssets":{
"self":"https://somebaseurl/inventory/managedObjects/5706682/childAssets",
"references":[]
},
"childAdditions":{
"self":"https://somebaseurl/inventory/managedObjects/5706682/childAdditions",
"references":[]
},
"name":"SOME",
"deviceParents":{
"self":"https://somebaseurl/inventory/managedObjects/5706682/deviceParents",
"references":[]
},
"assetParents":{
"self":"https://somebaseurl/inventory/managedObjects/5706682/assetParents",
"references":[]
},
"self":"https://somebaseurl/inventory/managedObjects/5706682",
"id":"5706682"
},
"self":"https://somebaseurl/inventory/managedObjects/5706999/assetParents/5706682"
}]
},
"self":"https://somebaseurl/inventory/managedObjects/5706999",
"id":"5706999",
"c8y_ActiveAlarmsStatus":{
"minor":0,
"critical":1
},
"c8y_IsDevice":{},
"ax_Customer":"SOME CUSTOMER",
"c8y_SupportedMeasurements":["c8y_Temperature"]}"))

除了解析单个属性之外,将整个对象映射到 Apama 事件的推荐方法是什么?

如果您能提供代码片段,我们将不胜感激。

非常感谢马蒂亚斯

最佳答案

要解决第二个问题,即“将整个对象映射到 Apama 事件的推荐方法是什么?”:

我定义了几个事件,它们将映射到您提供的 JSON:

event Reference {
string self;
any managedObject;
}

event Object {
string self;
sequence<Reference> references;
}

event ActiveAlarmsStatus {
integer minor;
integer critical;
}
event IsDevice {}

event ManagedObject {
string owner;
string self;
string id;
string name;
string creationTime;
string lastUpdated;
Object additionParents;
Object childDevices;
Object childAssets;
Object childAdditions;
Object deviceParents;
Object assetParents;
ActiveAlarmsStatus c8y_ActiveAlarmsStatus;
IsDevice c8y_IsDevice;
string ax_Customer;
sequence<string> c8y_SupportedMeasurements;
}

因为 ManagedObject 包含一个对象,而该对象包含一个引用,而该引用本身又包含一个 ManagedObject,因此 EPL 由于递归类型而无法编译。因此,在 Reference 中,我们使用 any 类型来屏蔽 ManagedObject。这允许 EPL 编译。

但是,因为 any 隐藏了类型,所以我们不知道将其转换为什么类型,因此我们有一个包含字典的 any 。不过这很好,因为我们可以使用一些辅助函数来提取我们想要的信息:

action GetSequenceReference(sequence<any> s) returns sequence<Reference> {
sequence<Reference> ret := new sequence<Reference>;
any r := new any;
for r in s {
ret.append(<Reference>r);
}
return ret;
}
action GetSequenceString(sequence<any> s) returns sequence<string> {
sequence<string> ret := new sequence<string>;
any r := new any;
for r in s {
ret.append(<string>r);
}
return ret;
}

action GetObject(any a) returns Object {
log "Getting object from: " + a.toString() at INFO;
dictionary<any,any> dict := <dictionary<any,any> >a;

Object ret := new Object;

ret.self := <string>dict.getDefault( "self", "" );
ret.references := GetSequenceReference( <sequence<any> >dict.getDefault( "references", new sequence<any> ) );

return ret;
}

action GetActiveAlarmsStatus(any a) returns ActiveAlarmsStatus {
log "Getting active alarms status from: " + a.toString() at INFO;
dictionary<any,any> dict := <dictionary<any,any> >a;

ActiveAlarmsStatus ret := new ActiveAlarmsStatus;

ret.minor := <integer>dict.getDefault( "minor", 0 );
ret.critical := <integer>dict.getDefault( "critical", 0 );

return ret;
}
action GetIsDevice(any a) returns IsDevice {
log "Getting is device from: " + a.toString() at INFO;
dictionary<any,any> dict := <dictionary<any,any> >a;

IsDevice ret := new IsDevice;

return ret;
}
action GetManagedObject(any a) returns ManagedObject {

log "Getting managed object from: " + a.toString() at INFO;
dictionary<any,any> dict := <dictionary<any,any> >a;

ManagedObject ret := new ManagedObject;
ret.owner := <string>dict.getDefault( "owner", "" );
ret.self := <string>dict.getDefault( "self", "" );
ret.id := <string>dict.getDefault( "id", "" );
ret.name := <string>dict.getDefault( "name", "" );
ret.creationTime := <string>dict.getDefault( "creationTime", "" );
ret.lastUpdated := <string>dict.getDefault( "lastUpdated", "" );
ret.additionParents := GetObject( dict.getDefault( "additionParents", new dictionary<any,any> ) );
ret.childDevices := GetObject( dict.getDefault( "childDevices", new dictionary<any,any> ) );
ret.childAssets := GetObject( dict.getDefault( "childAssets", new dictionary<any,any> ) );
ret.childAdditions := GetObject( dict.getDefault( "childAdditions", new dictionary<any,any> ) );
ret.deviceParents := GetObject( dict.getDefault( "deviceParents", new dictionary<any,any> ) );
ret.assetParents := GetObject( dict.getDefault( "assetParents", new dictionary<any,any> ) );
ret.c8y_ActiveAlarmsStatus := GetActiveAlarmsStatus( dict.getDefault( "c8y_ActiveAlarmsStatus", new dictionary<any,any> ) );
ret.c8y_IsDevice := GetIsDevice( dict.getDefault( "c8y_IsDevice", new dictionary<any,any> ) );
ret.ax_Customer := <string>dict.getDefault( "ax_Customer", "" );
ret.c8y_SupportedMeasurements := GetSequenceString( <sequence<any> >dict.getDefault( "c8y_SupportedMeasurements", new sequence<any> ) );
return ret;
}

使用这些辅助函数,我们可以从原始事件中提取信息:

action handleResponse(Response response){
if response.isSuccess(){
ManagedObject mo := GetManagedObject( response.payload.toDictionary() );
log "###The response payload is :" + mo.toString() at INFO;
}

else {
log "###Request failed. Response status is: " + response.statusCode.toString() + " | " + response.statusMessage at INFO;
}
}

这就是你得到的!完全映射的事件。

关于apama - 如何解析 HTTP JSON 响应负载中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com