繁体   English   中英

如何在 apache camel 中将 csv 转换为 json

[英]How to convert csv to json in apache camel

我能够在 camel bindy 的帮助下将 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");

    }
}

很想知道是否有任何有效的方法可以解决这个问题?

.json()

查看camel 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");

其中json(JsonLibrary.Jackson)强制使用 jackson 库进行转换。

  1. 您可以使用 camel bindy t unmarshal csv 到 POJO 列表
  2. 如果需要,根据输出格式进行映射
  3. 在 Came 路由中使用 Jacson o Gson 编组映射数据。
    from("file:/input/path/?noop=true")
    .unmarshal(bindy)
    .marshal()
    .json(JsonLibrary.Jackson).log("${body}")
    .to("file:/path/?fileName=test.txt");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM