我能够在骆驼绑定的帮助下将 csv 转换为 pojo。但我需要轻松地将字符串转换为 json 吗?我可以拆分字符串。但是有什么有效的方法可以做到吗?我的pojo类:-@CsvRecord(separator = ",",skipFirstLine = true)public class Sample{ //some fields}处理器类:-String samples=exchange.getIn().getBody(String.class);String[] strings=samples.split("}");System.out.println(strings[0]);for(String strings1:strings){ String[] strings2=strings1.split("'"); for(int i=0;i<strings2.length;i++) { if(i%2==1) { System.out.println(strings2[i]); } }}//Is there is efficient method we can do to convert the String to list of json. Assuming csv contains multiple record路线建设者: -public class SimpleRouteBuilder extends RouteBuilder { private final BindyCsvDataFormat bindy=new BindyCsvDataFormat(com.company.model.Sample.class);; @Override public void configure() { from("file:/path/?fileName=CCO_SUMMARY_20190315_165800 copy.csv&noop=true").unmarshal(bindy).process(new MyProcessor()). to("file:/path/?fileName=akshay.txt"); }}想知道是否有任何有效的方法来解决这个问题?
2 回答

红糖糍粑
TA贡献1815条经验 获得超6个赞
.json()
请参阅骆驼 json 数据格式, 即:.json().log("${body}")
例如,在您的场景中:
from("file:/path/?fileName=CCO_SUMMARY_20190315_165800 copy.csv&noop=true") .unmarshal(bindy) .marshal() .json(JsonLibrary.Jackson).log("${body}") .to("file:/path/?fileName=akshay.txt");
wherejson(JsonLibrary.Jackson)
强制使用 jackson 库进行转换。

白板的微信
TA贡献1883条经验 获得超3个赞
您可以使用 camel bindy t unmarshal csv to List of POJO
如果需要,根据输出格式进行映射
在 Came 路线中使用 Jacson o Gson 编组映射数据。
从(“文件:/输入/路径/?noop = true”)
.unmarshal(绑定)
。元帅()
.json(JsonLibrary.Jackson).log("${body}")
.to("file:/path/?fileName=test.txt");
添加回答
举报
0/150
提交
取消