【问题标题】:Can not Extract json array properly in message Enricher mule esb无法在消息 Enricher mule esb 中正确提取 json 数组
【发布时间】:2015-04-29 20:01:03
【问题描述】:

我尝试使用消息丰富器提取 json 数组数据。但是在输出中我得到了一些对象数据。如何正确获取?

以下是我的流程

<flow name="readfileFlow1" doc:name="readfileFlow1">
        <file:inbound-endpoint path="Test" moveToDirectory="Backup" responseTimeout="10000" doc:name="File"/>
        <enricher source="#[(name in payload.data)]" target="#[flowVars.myMap]" doc:name="Message Enricher">
            <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        </enricher>
        <logger message="#[payload.toString()]" level="INFO" doc:name="Logger"/>
</flow>

当我在记录器中打印时,我得到以下输出

INFO  2015-04-29 13:01:20,409 [[readfile].readfileFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor: org.mule.transport.file.ReceiverFileInputStream@15bc5cc

如何正确提取所有名称 我尝试使用每个 .使用 for each 我可以成功提取。但是想使用消息丰富器。如何使用消息丰富器 MVEL 做到这一点??

我的json数据如下..

{
   "data":[
       {
            "id" : "1",
    "name": "AAA"

        },
        {
            "id" : "5",
                "name": "DDD"
        },
        {
            "id" : "6",
                "name": "CCC"
        },


    ]
}

【问题讨论】:

    标签: mule mule-studio mule-component


    【解决方案1】:

    我已经做到了.. 我犯的愚蠢错误.. 以下是我的回答

    <logger message="#[flowVars.myMap]" level="INFO" doc:name="Logger"/>
    

    【讨论】:

      【解决方案2】:

      这是从 JSON 有效负载中提取名称和 ID 的方法。您将在记录器中获取它

         <flow name="readfileFlow1" doc:name="readfileFlow1">
           <file:inbound-endpoint path="Test" moveToDirectory="Backup" responseTimeout="10000" doc:name="File"/> 
           <enricher source="#[message.payload]" target="#[flowVars.myMap]" doc:name="Message Enricher">
             <processor-chain doc:name="Processor Chain">
             <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
             <foreach collection="#[payload.data]">
             <logger level="INFO" message="Name :- #[payload.name] and Id:- #[payload.id]"/>
             </foreach>
             </processor-chain>
            </enricher>
           <logger level="INFO" message="#[flowVars.myMap]"/>     
          </flow>
      

      您的 JSON 有效负载无效的另一件事.. 尝试在末尾删除额外的 , .. 这样您的有效 JSON 文件将是:-

      {
         "data":[
             {
                  "id" : "1",
          "name": "AAA"
      
              },
              {
                  "id" : "5",
                      "name": "DDD"
              },
              {
                  "id" : "6",
                      "name": "CCC"
              }
          ]
      }
      

      【讨论】:

      • 感谢代表。但我已经为每个都做了。我想用 MVEL 消息丰富器来做
      • 你应该在你的问题中提到你想用 MVEL 来做这件事。没有你在你的要求中写的地方
      • 是的,正确..但您需要提及您想要解决它的方法..如果您没有明确提及您想要解决的方法,人们会为您提供帮助: ) 无论如何.. :)
      • 看看stackoverflow.com/questions/21589428/…我想你可以从这里得到一些帮助
      猜你喜欢
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      相关资源
      最近更新 更多