简体   繁体   English

在Mule 3.2流程中解析JSON

[英]Parse JSON in Mule 3.2 Flow

If I have the following JSON 如果我有以下JSON

[ 
  { "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
  },
  { "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
  },
  { "category": "fiction",
    "author": "Herman Melville",
    "title": "Moby Dick",
    "isbn": "0-553-21311-3",
    "price": 8.99
  },
  { "category": "fiction",
    "author": "J. R. R. Tolkien",
    "title": "The Lord of the Rings",
    "isbn": "0-395-19395-8",
    "price": 22.99
  }
]

I can get the price of "Moby Dick" using the following JSONPath: 我可以使用以下JSONPath获得“Moby Dick”的价格:

$..[?(@.title=='Moby Dick')].price

RESULT: 结果:

'0' => "8.99"

BUT how do I do this in Mule..... what would the json expression look like to get the same result within a mule 3.2 flow? 但是我如何在Mule中做到这一点...... json表达式在mule 3.2流程中得到相同的结果是什么样的?

I don't mind if your answer shows how to do it another way as long as I get the same result within mule. 我不介意你的答案显示如何以另一种方式做到这一点,只要我在mule中获得相同的结果。

Can anyone please help me with this?? 有人可以帮我这个吗?

The way to do that with MEL is to deserialize the JSON payload and use an MVEL filtered projection : 使用MEL的方法是反序列化JSON有效负载并使用MVEL过滤投影

<json:json-to-object-transformer returnClass="java.util.List" />
<expression-transformer 
    expression="#[($.price in message.payload if $.title == 'Moby Dick')[0]]" />

This expression doesn't take into account cases when Moby Dick isn't present. 该表达式没有考虑Moby Dick不存在的情况。 You didn't mention what to do in that case: I can beef up the expression if you specify the desired behavior when the book's not found. 在这种情况下你没有提到要做什么:如果你在找不到书时指定了所需的行为,我可以加强表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM