Apache Camel split JSONArray remove the double quote

落花浮王杯 提交于 2020-01-17 05:48:26

问题


I've split the JSONArray using JsonPathExpression, but the result removed every double quote in each JSON, here is my RouteBuilder.

from("timer:scheduler?repeatCount=1")
    .to("http:localhost:8901/rest/getData")
        .split(new JsonPathExpression("$.[*]"))
        .process(new Processor() {
            @java.lang.Override
            public void process(Exchange exchange) throws Exception {
                String input = exchange.getIn().getBody(String.class);
                exchange.getIn().setBody(input);
            }
        })
        .unmarshal().json(JsonLibrary.Jackson, Map.class)
    .log("${body}");

I just want to unmarshal those JSON using Jackson, but as you might know, Jackson will not parse those JSON without double quote, yes, I might allow Jackson to parse without double quote, but I prefer the JSON to have it. Also it changes the : to =.

The response from REST call are like this.

[
  {
    "v_KDKANWIL": null,
    "v_GJJ": "CRJ"
  },
  {
    "v_KDKANWIL": "002",
    "v_GJJ": "CRJ"
  }
]

After splitting and processing it become like this (Just the first JSON).

{
  v_KDKANWIL=null,
  v_GJJ=CRJ
}

I suspect not just the split, but also in the processor because I set new body using String type.

Thanks in advance.

EDIT

Ah, stupid me, it should not be a String but Map, so this.

String input = exchange.getIn().getBody(String.class);

Become this.

Map input = exchange.getIn().getBody(String.class);

回答1:


`.split()` splits data into java.util.map. Marshal data after split to convert it to JSON using Jackson or Gson. I faced the same issue and adding `marshal()` resolved the issue.

.marshal().json(JsonLibrary.Jackson)


来源:https://stackoverflow.com/questions/39981444/apache-camel-split-jsonarray-remove-the-double-quote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!