JSON数组的JSON评估问题

huangapple go评论78阅读模式
英文:

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)]*

huangapple
  • 本文由 发表于 2023年3月8日 17:54:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定