Convert a JSON to TW Object of type ANY

混江龙づ霸主 提交于 2019-12-11 17:05:07

问题


Using IBM BPM 8.6 I have a JSON as follows:

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

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

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

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

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

How can I get the output in an any object?

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


回答1:


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.

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"}';

I tried -

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

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.

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



来源:https://stackoverflow.com/questions/53003790/convert-a-json-to-tw-object-of-type-any

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!