将JSON转换为ANY类型的TW对象 [英] Convert a JSON to TW Object of type ANY

查看:245
本文介绍了将JSON转换为ANY类型的TW对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用IBM BPM 8.6 我有一个JSON,如下所示:

Using IBM BPM 8.6 I have a JSON as follows:

tw.local.person = "{\"firstName\":\"Ahmed\",\"job\":\"Doctor\"}";

我正在使用BPM帮助程序工具包将json转换为TW Object

I am using the BPM helper toolkit to convert the json to TW Object

tw.local.outputObject = BPMJSON.convertJSONToTw(tw.local.person); 

结果:
如果outputObject的类型为Person(具有firstName和job属性),则它将正常工作并创建对象.

RESULTS:
If the outputObject is of type Person (with the attributes firstName and job), it works and the object is created.

如果outputObject的类型为any,则它不起作用

If the outputObject is of type any, it doesn't work

如何在any对象中获取输出?

How can I get the output in an any object?

在BPM-JSON-Utils.js或json2.js文件中是否有任何变通办法或调整?

Any workaround or a a tweak in the BPM-JSON-Utils.js or json2.js files?

推荐答案

首先要注意的是,在我的8.6安装中,调用JSON.parse()可以正常工作,因此不需要社区工具包.需要注意的是,这种方法似乎遇到了与尝试使用ANY或Record进行操作时所看到的错误相同的错误.

The first thing I would note that in my 8.6 install, calling JSON.parse() just works, so you don't need the community toolkit. That being noted, that approach seems to encounter what is likely the same bug as you are seeing when you try to do it using ANY or Record.

基于该错误,似乎基础TWObject不允许您引用未明确声明的成员字段.在我的测试中,使用JSON字符串-

Based on the error it seems that the underlying TWObject won't let you reference member fields that are not explicitly declared. In my tests, using the JSON String -

var json='{ "name" : "Andrew", "value" : "42"}';

我尝试-

tw.local.myNvp = JSON.parse(json);
tw.local.myAny = JSON.parse(json);

第一个从系统数据工具包解析为"NameValuePair"类型的变量的方法起作用了.试图解析为"ANY"的第二个失败.我还尝试了Record,看看我们是否可以到达那里,但是那也失败了.

The first one which was parsing into a variable of type "NameValuePair" from the system data toolkit worked. The 2nd which was trying to parse into an "ANY" failed. I also tried with Record to see if we could get there, but that failed as well.

我的建议是将原始JSON返回给调用方,并让它们调用上面的解析行.我假设调用者期望返回一个特定的类型,这意味着该变量不是抽象类型,因此解析调用应该可以正常工作.

My suggestion would be to return the raw JSON to the caller and have them invoke the parse line above. I'm assuming the caller is expecting a specific type back, which means the variable isn't an Abstract type, so the parse call should work.

-安德鲁·派尔(Andrew Paier)

-Andrew Paier

这篇关于将JSON转换为ANY类型的TW对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆