英文:
JSON Evaluation issue with JSON Array
问题
在您提供的内容中,我会提取并翻译其中的文本部分,而不翻译代码部分。以下是翻译后的文本:
We are using JSON library https://github.com/octomix/josson
Also Refer issue and answer on https://stackoverflow.com/questions/75563953/evaluate-json-object-recursively
My sample JSON
{
"data": {
"A": "717.5",
"B": {
"expression": "calc(205+42*A,A:$.selectedPump.stg)"
},
"E": {
"expression": "calc(A+B)"
},
"D": "96",
"D1": "96",
"MotorWt": "28.5"
},
"selectedPump": {
"termsHtrimSolarMotor": [
-7.372112927801618,
8.837084474924652,
-1.1152401096382207
],
"overallEffEq": "[-0.0013252914566755647, 0.10052143733055335, -0.005975385931287854]",
"stg": 1
}
}
JAVA Code to calculate only when the formula is given in "expression"
String json = "{\"data\":{\"A\":\"717.5\",\"B\":{\"expression\":\"calc(205+42*A,A:$.selectedPump.stg)\"},\"E\":{\"expression\":\"calc(A+B)\"},\"D\":\"96\",\"D1\":\"96\",\"MotorWt\":\"28.5\"},\"selectedPump\":{\"termsHtrimSolarMotor\":[-7.372112927801618,8.837084474924652,-1.1152401096382207],\"overallEffEq\":\"[-0.0013252914566755647,0.10052143733055335,-0.005975385931287854]\",\"stg\":1}}";
Josson josson = Josson.fromJsonString(json);
String expression = josson.getString(
"entries()" +
".concat('field(',key, value.entries()" +
" .[value.expression!=null]*" +
" .concat('.field(',key,': eval(',key,'.expression))') " +
" .join()," +
" ')')" +
".join('.')");
System.out.println(expression);
JsonNode node = josson.getNode(expression);
System.out.println(node.toString());
Output
field(data.field(B: eval(B.expression)).field(E: eval(E.expression))).field(selectedPump.field(termsHtrimSolarMotor: eval(termsHtrimSolarMotor.expression)))
{"data":{"A":"717.5","B":247.0,"E":964.5,"D":"96","D1":"96","MotorWt":"28.5"},"selectedPump":{"termsHtrimSolarMotor":[],"overallEffEq":"[-0.0013252914566755647,0.10052143733055335,-0.005975385931287854]","stg":1}}
here the key "termsHtrimSolarMotor" which is an array is evaluated even if it doesn't have an expression and gives an empty array in the result
"termsHtrimSolarMotor":[]
Thanks
Pranav
英文:
We are using JSON library https://github.com/octomix/josson
Also Refer issue and answer on https://stackoverflow.com/questions/75563953/evaluate-json-object-recursively
My sample JSON
{
"data": {
"A": "717.5",
"B": {
"expression": "calc(205+42*A,A:$.selectedPump.stg)"
},
"E": {
"expression": "calc(A+B)"
},
"D": "96",
"D1": "96",
"MotorWt": "28.5"
},
"selectedPump": {
"termsHtrimSolarMotor": [
-7.372112927801618,
8.837084474924652,
-1.1152401096382207
],
"overallEffEq": "[-0.0013252914566755647, 0.10052143733055335, -0.005975385931287854]",
"stg":1
}
}
JAVA Code to calculate only when the formula is given in "expression"
String json="{\"data\":{\"A\":\"717.5\",\"B\":{\"expression\":\"calc(205+42*A,A:$.selectedPump.stg)\"},\"E\":{\"expression\":\"calc(A+B)\"},\"D\":\"96\",\"D1\":\"96\",\"MotorWt\":\"28.5\"},\"selectedPump\":{\"termsHtrimSolarMotor\":[-7.372112927801618,8.837084474924652,-1.1152401096382207],\"overallEffEq\":\"[-0.0013252914566755647,0.10052143733055335,-0.005975385931287854]\",\"stg\":1}}";
Josson josson = Josson.fromJsonString(json);
String expression = josson.getString(
"entries()" +
".concat('field('," +
" key," +
" value.entries()" +
" .[value.expression!=null]*" +
" .concat('.field(',key,': eval(',key,'.expression))')" +
" .join()," +
" ')')" +
".join('.')");
System.out.println(expression);
JsonNode node=josson.getNode(expression);
System.out.println(node.toString());
Output
field(data.field(B: eval(B.expression)).field(E: eval(E.expression))).field(selectedPump.field(termsHtrimSolarMotor: eval(termsHtrimSolarMotor.expression)))
{"data":{"A":"717.5","B":247.0,"E":964.5,"D":"96","D1":"96","MotorWt":"28.5"},"selectedPump":{"termsHtrimSolarMotor":[],"overallEffEq":"[-0.0013252914566755647,0.10052143733055335,-0.005975385931287854]","stg":1}}
here the key "termsHtrimSolarMotor" which is a array is evaluated even if it doesn't have expression and give empty array in result
"termsHtrimSolarMotor":[]
Thanks
Pranav
答案1
得分: 1
在 [value.expression!=null]*
中,value.expression
对于 termsHtrimSolarMotor 是一个空数组,因此它不是null。
请尝试 [isText(value.expression)]*
。
英文:
In [value.expression!=null]*
, value.expression
for termsHtrimSolarMotor is an empty array and therefore it is not null.
Please try [isText(value.expression)]*
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论