读取 JSON 文件内容使用 POLLENRICH 的方法是什么?

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

How do i read a JSON file content using POLLENRICH

问题

I'm working with Apache Camel version 2.24.3, and this is my JSON file content:

{
"p-1": "smh_5000",
"p-2": "smh_6000"
}

So I want to read this content file using PollEnrich. When I give it "p-1," it returns "smh_5000." May someone please advise on how to use PollEnrich with this example.

英文:

I'm working with Apache camel version 2.24.3 and this is my JSON file content

{
  "p-1":"smh_5000",
  "p-2":"smh_6000"
}

So i want to read this content file using pollenrich: when i give him p-1,he returns smh_5000.
May someone please advice on how to use pollenrinch with this example.

答案1

得分: 0

Sure, here's the translated content:

<camelContext>
    <pollEnrich id="readFile" timeout="5000">
        <constant>file://data?fileName=test.json</constant>
    </pollEnrich>
    <log id="jsonData" message="The message contains ${body}"/>
    <process id="readingparams" ref="ReadingP"/>
</camelContext>

And your Java code:

public class ReadingP implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception, JSONException {
        JSONObject json = (new JSONObject(exchange.getIn().getBody(String.class))).getJSONArray("catalogue").getJSONObject(0);
        Map<String,Object> ctx = exchange.getProperties();
        ctx.put("product-p", json.get("p-1"));
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    }
}

Please note that I've replaced the HTML entities with their respective characters in the translated content.

英文:
&lt;camelContext&gt; 
&lt;pollEnrich id=&quot;readFile&quot; timeout=&quot;5000&quot;&gt; 
                &lt;constant&gt;file://data?fileName=test.json&lt;/constant&gt; 
            &lt;/pollEnrich&gt; 
     &lt;log id=&quot;jsonData&quot; message=&quot;The message contains ${body}&quot;/&gt; 
     &lt;process id=&quot;readingparams&quot; ref=&quot;ReadingP&quot;/&gt; 
&lt;/camelContext&gt;

and my process is :

public class ReadingP implements Processor { 
	@Override 
	public void process(Exchange exchange) throws Exception,JSONException {
		        JSONObject json = (new JSONObject(exchange.getIn().getBody(String.class))).getJSONArray(&quot;catalogue&quot;).getJSONObject(0);
		        Map&lt;String,Object&gt; ctx= exchange.getProperties();
				ctx.put(&quot;product-p&quot;, json.get(&quot;p-1&quot;));
				exchange.getOut().setHeaders(exchange.getIn().getHeaders());
         }
} 

huangapple
  • 本文由 发表于 2020年8月11日 19:44:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63357438.html
匿名

发表评论

匿名网友

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

确定