简体   繁体   English

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

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

I able to convert csv into pojo with the help of camel bindy.我能够在 camel bindy 的帮助下将 csv 转换为 pojo。 But I need to convert the string to json easily?但是我需要轻松地将字符串转换为 json 吗?

I able to do my splitting the string.But is there any efficient methods to do it?我可以拆分字符串。但是有什么有效的方法吗?

My pojo Class:-我的 pojo 类:-

@CsvRecord(separator = ",",skipFirstLine = true)

public class Sample
{

 //some fields

}

Processor Class:-处理器类:-

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

Route Builder:-路线建设者:-

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");

    }
}

Eager to know if there is any efficient method to solve this?很想知道是否有任何有效的方法可以解决这个问题?

.json() .json()

see camel json data format ie: .json().log("${body}")查看camel json 数据格式即: .json().log("${body}")

In your scenario for example:例如,在您的场景中:

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");

where json(JsonLibrary.Jackson) forces the use of the jackson library for the conversion.其中json(JsonLibrary.Jackson)强制使用 jackson 库进行转换。

  1. You can use camel bindy t unmarshal csv to List of POJO您可以使用 camel bindy t unmarshal csv 到 POJO 列表
  2. Do mapping if required base on output format如果需要,根据输出格式进行映射
  3. marshal mapped data using Jacson o Gson in Came route.在 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.

相关问题 将 JSON 数组转换为 Object 数组 - Apache 骆驼 - Convert JSON array to Object Array - Apache camel Apache Camel:使用 Camel 方法将 JSON 转换为 POJO - Apache Camel: Convert JSON to a POJO using Camel methods 如何在Apache Camel中记录csv的内容? - How to log the content of csv in Apache Camel? 如何使用附加字段丰富 csv 内容数据,并使用 apache camel 将管道作为分隔符转换为文本文件 - How to enrich the csv content data with additional fields and convert into text file with pipe as delimiter using apache camel Apache骆驼-SQL到CSV - Apache camel - SQL to CSV Apache Camel如何将数据库响应转换为XML - Apache Camel How convert DB response to XML 如何在Apache骆驼中将XML转换为Java对象? - How to convert XML to Java object in Apache camel? 使用 Apache Camel 从另一个 API 使用 JSON 并将其写入 CSV - Consume JSON from another API using Apache Camel and write it in CSV 如何使用Apache Camel XmlJsonDataFormat编组将XML字符串转换为JSON字符串 - How to Convert XML string to JSON string using Apache Camel XmlJsonDataFormat marshalling 如何编写 Apache 骆驼路线来检查 XML 内容并将其转换为 JSON 和 Z3418008FDD81C28 引导服务反之亦然 - How to write a Apache Camel route to check and convert XML contents to JSON and vice-versa for a Spring boot service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM