在Mule 3.2流中解析JSON [英] Parse JSON in Mule 3.2 Flow

查看:180
本文介绍了在Mule 3.2流中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下JSON

If I have the following 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
  }
]

我可以使用以下JSONPath获取Moby Dick的价格:

I can get the price of "Moby Dick" using the following JSONPath:

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

结果:

'0' => "8.99"

但是如何在Mule中执行此操作?json表达式看起来要在骡子3.2流程中获得相同的结果?

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?

我不介意,如果你的答案显示如何做到另一种方式,只要我得到相同

I don't mind if your answer shows how to do it another way as long as I get the same result within mule.

任何人都可以帮助我吗?

Can anyone please help me with this??

推荐答案

使用MEL进行此操作的方式是反序列化JSON有效载荷,并使用 MVEL过滤投影

The way to do that with MEL is to deserialize the JSON payload and use an MVEL filtered projection:

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

这个表达式没有考虑到 Moby Dick 不存在。你没有提到在这种情况下该做什么:如果您在找不到该书时指定了所需的行为,我可以加强表达式。

This expression doesn't take into account cases when Moby Dick isn't present. 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.

这篇关于在Mule 3.2流中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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