使用jackson将csv转换为json

时间:2018-05-14 15:35:50

标签: json csv jackson apache-camel

之前没有这样做过。寻找一点方向和帮助。谢谢大家。

我需要获取一个csv数据文件并将其转换为特定格式的json文件。我现在正在阅读JsonObject和JsonArray,...谢谢!

我有类似的东西。

timestamp,NST_MD3_CRANK_CASE,NST_MD3-TAH-0001,NST_MD3-TAH-0002,NST_MD3-TAH-0003,NST_MD3-TAH-0004,NST_MD3-TAH-0005,NST_MD3-TAH-0006,NST_MD3-TAH-0007,NST_MD3-TAH-0008,NST_MD3-TAH-0009,NST_MD3-TAH-0010,NST_MD3-TAH-0011,NST...
4/10/2018 7:55:10 PM,101,261,258,276,267,274,279,273,286,273,276,288,292,268,297,285,316,81,136,100,0,0,388,395,84,956,20,1,0,100,0,17,0,31,16,31,18,23,694.7667,59.98439,0,0,0,1,14998,13,24
4/10/2018 7:58:10 PM,101,261,258,276,267,274,279,273,286,273,276,288,292,268,297,285,316,81,136,100,0,0,388,395,84,956,20,1,0,100,0,17,0,31,16,31,18,23,694.9326,59.92459,0,0,0,1,14998,13,24

我需要像这样的Json格式......

{ "tags": [ {"tagId": "NST_MD1_CRANK_CASE","data":[ {"ts":"2017-11-15T19:55:00","v" : "100","q":"3"}] }, ... ] }

我使用的是camel,java和jackson。再次感谢!

2 个答案:

答案 0 :(得分:0)

这是使用org.json:如果你需要添加更多数据,只需向JSONArray添加更多对象(data2,data3 ...)。使用JsonArrays时要小心括号!

预期产出:

{
"tags": [{
    "data": [{
        "q": "3",
        "v": "100",
        "ts": "2017-11-15T19:55:00"
    }],
    "tagId": "NST_MD1_CRANK_CASE"
}]

}

这是代码:

package test;

import org.json.JSONArray;
import org.json.JSONObject;

public class jsonExample {


    public static void main(String[] args) {
        //create our data
        String tagId = "NST_MD1_CRANK_CASE";
        JSONObject data1 = new JSONObject();        
        data1.put("ts", "2017-11-15T19:55:00");
        data1.put("v", "100");
        data1.put("q", "3");

        //create dataArray -> JSONArray
        /*
          [{ "ts": "2017-11-15T19:55:00",
             "v": "100",
             "q": "3" }] 
         */
        JSONArray dataArray = new JSONArray();
        dataArray.put(data1);

        //create internal JSON Object
        /*  
          {
            "tagId": "NST_MD1_CRANK_CASE",
            "data": [{
                "ts": "2017-11-15T19:55:00",
                "v": "100",
                "q": "3"
            }]
        }
         */
        JSONObject internal = new JSONObject();
        internal.put("tagId", tagId);
        internal.put("data", dataArray);

        //create tags -> JSON Array
        /*  
            [{
                "tagId": "NST_MD1_CRANK_CASE",
                "data": [{
                    "ts": "2017-11-15T19:55:00",
                    "v": "100",
                    "q": "3"
                }]
            }]
         */
        JSONArray tags = new JSONArray();
        tags.put(internal);

        //create final JSON Object
        /*
          {
            "tags": [{
                "tagId": "NST_MD1_CRANK_CASE",
                "data": [{
                    "ts": "2017-11-15T19:55:00",
                    "v": "100",
                    "q": "3"
                }]
            }]
         }
         */
        JSONObject finalJson = new JSONObject();
        finalJson.put("tags", tags);
        System.out.println(finalJson.toString());

    }


}

答案 1 :(得分:0)

使用杰克逊库,我想出了如何使用JsonFactory和JsonGenerator以逐步方式实现它。谢谢大家。

代码:

public class JsonMapper implements Processor {

    private int noOfRows = 0;  //does not include the header record
    private int noOfColumns = 0;  //includes timestamp field

    private static Logger log = LoggerFactory.getLogger(JsonMapper.class.getName());

    @Override
    public void process(Exchange exchange) throws Exception {

        log.info("Entering Json Mapper Processor...");

        String body = exchange.getIn().getBody(String.class);
        List<String> serviceRecords = new ArrayList<String>(Arrays.asList(body.split(System.lineSeparator())));
        noOfRows = serviceRecords.size() - 1;
        noOfColumns = getColumns(serviceRecords);
        StringBuilder header = getHeader( serviceRecords );
        serviceRecords.remove(0); //remove header record only data records in list
        String hdr[] = header.toString().split(",");
        //Noble_nst_mud-pump-2_NST_MUDPIT1_VOL,Noble_nst_mud-pump-2_NST_MUDPIT2_VOL,Noble_nst_mud-pump-2_NST_MUDPIT3_VOL,Noble_nst_mud-pump-2_NST_MUDPIT4_VOL,Noble_nst_mud-pump-2_NST_MUDPIT5_VOL,Noble_nst_mud-pump-2_NST_MUDPIT6_VOL,Noble_nst_mud-pump-2_NST_MUDPIT7_VOL,Noble_nst_mud-pump-2_NST_MUDPIT8_VOL,Noble_nst_mud-pump-2_NST_R_MUDPIT1_VOL,Noble_nst_mud-pump-2_NST_R_MUDPIT2_VOL,Noble_nst_mud-pump-2_NST_R_MUDPIT3_VOL,Noble_nst_mud-pump-2_NST_R_MUDPIT4_VOL,Noble_nst_mud-pump-2_NST_MP2_MOT_RUN_001,Noble_nst_mud-pump-2_NST_MP2_AI_021,Noble_nst_mud-pump-2_NST_MP2_AI_023,Noble_nst_mud-pump-2_NST_MP2_AI_022,Noble_nst_mud-pump-2_NST_MP2_AI_004,Noble_nst_mud-pump-2_NST_MP2_AI_005,Noble_nst_mud-pump-2_NST_MP2_AI_006,Noble_nst_mud-pump-2_NST_MP2_AI_031,Noble_nst_mud-pump-2_NST_MP2_AI_033,Noble_nst_mud-pump-2_NST_MP2_AI_032,Noble_nst_mud-pump-2_NST_MP2_AI_011,Noble_nst_mud-pump-2_NST_MP2_AI_012,Noble_nst_mud-pump-2_NST_MP2_AI_013,Noble_nst_mud-pump-2_NST_MP2_MOT_RUN_010,Noble_nst_mud-pump-2_NST_MP2_MOT_RUN_004,Noble_nst_mud-pump-2_NST_MP2_MOT_RUN_005,Noble_nst_mud-pump-2_NST_MP2_MOT_RUN_006,Noble_nst_mud-pump-2_NST_MP2_MOT_RUN_009
        //04/10/2018 07:55:10 PM, 1, 0, 2, 0, 81, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 289.4146, 288.65, 288.65, 0, 0, 0, 288.9146, 291.15, 289.2283, 0, 0, 0, 0, 0
        //String [] record = serviceRecords.toArray(new String[serviceRecords.size()]);

        JsonFactory factory = new JsonFactory();
        StringWriter sw = new StringWriter();
        JsonGenerator generator = factory.createGenerator(sw);
        generator.setPrettyPrinter(new DefaultPrettyPrinter());

        generator.writeStartObject();  // {
        generator.writeFieldName("tags"); // {"tags":
        generator.writeStartArray(); // {"tags": [

        for ( String rec : serviceRecords ) {
            String[] data = rec.split(",");

            for ( int i = 0; i < (noOfColumns - 1); ++i ) {  //header has 1 less field than data record         
                generator.writeStartObject(); // {
                generator.writeStringField("tagId", hdr[i].trim()); // { "tagId": "NST_MD1_CRANK_CASE" 
                generator.writeFieldName("data"); // { "tagId": "NST_MD1_CRANK_CASE", "data"
                generator.writeStartArray(); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [
                generator.writeStartObject(); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{
                generator.writeStringField("ts", data[0].trim()); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00"
                generator.writeStringField("v", data[i+1].trim()); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100"
                generator.writeStringField("q", "3"); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100","q":"3"
                generator.writeEndObject(); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100","q":"3"}
                generator.writeEndArray();  // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100","q":"3"}]
                generator.writeEndObject(); // { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100","q":"3"}]}
            }
        }
        generator.writeEndArray(); // {"tags": [ { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100","q":"3"}]} ]
        generator.writeEndObject();  // {"tags": [ { "tagId": "NST_MD1_CRANK_CASE", "data" : [{"ts":"2017-11-15T19:55:00","v" : "100","q":"3"}]} ]}
        generator.flush();
        generator.close();

        log.info("JSON: " + sw.toString());

        exchange.getIn().setBody(sw.toString());
        sw.close();
        log.info("Leaving Json Mapper Processor...");

   }

    public StringBuilder getHeader(List<String> serviceRecords) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i < 1; i++ ) {
            log.debug( "count: " + i + " record: : " + serviceRecords.get(i).toString() );
            if ( i == 0 ) {
                String[] sa = serviceRecords.get(i).toString().split(",");
                for ( int j = 0; j < sa.length; ++j) {
                    if ( j != 0 ) {
                        sb.append(sa[j]).append(",");
                    }
                }
                sb.deleteCharAt(sb.lastIndexOf(",", sb.length())); 
            } else {
                break;
            }
        }
        return sb;
    }

    public int getColumns(List<String> serviceRecords) {
        int columns = 0;
        for ( int i = 0; i < 1; i++ ) {
            log.debug( "count: " + i + " record: : " + serviceRecords.get(i).toString() );
            if ( i == 0 ) {
                String[] sa = serviceRecords.get(i).toString().split(",");
                columns = sa.length;
            } else {
                break;
            }
        }
        return columns;
    }

}