Apache Camel split JSONArray remove the double quo

2019-09-15 11:45发布

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条回答
Viruses.
2楼-- · 2019-09-15 12:08
`.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)
查看更多
登录 后发表回答