英文:
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.
英文:
<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 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("catalogue").getJSONObject(0);
Map<String,Object> ctx= exchange.getProperties();
ctx.put("product-p", json.get("p-1"));
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论