Apache Camel 拆分 JSONArray 去除双引号 [英] Apache Camel split JSONArray remove the double quote

查看:26
本文介绍了Apache Camel 拆分 JSONArray 去除双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JsonPathExpression 拆分了 JSONArray,但结果删除了每个 JSON 中的每个双引号,这是我的 RouteBuilder.

from("timer:scheduler?repeatCount=1").to("http:localhost:8901/rest/getData").split(new JsonPathExpression("$.[*]")).process(新处理器(){@java.lang.Overridepublic void process(Exchange exchange) 抛出异常 {字符串输入 = exchange.getIn().getBody(String.class);exchange.getIn().setBody(input);}}).unmarshal().json(JsonLibrary.Jackson, Map.class).log("${body}");

我只想使用 Jackson 解组那些 JSON,但您可能知道,Jackson 不会解析那些没有双引号的 JSON,是的,我可能允许 Jackson 解析没有双引号,但我更喜欢 JSON 有它.它还将 : 更改为 =.

REST 调用的响应是这样的.

<预><代码>[{v_KDKANWIL":空,"v_GJJ": "CRJ"},{"v_KDKANWIL": "002","v_GJJ": "CRJ"}]

拆分和处理后就变成这样了(只是第一个JSON).

<代码>{v_KDKANWIL=null,v_GJJ=CRJ}

我怀疑不仅是拆分,还有处理器,因为我使用 String 类型设置了新主体.

提前致谢.

编辑

啊,笨蛋,应该不是String而是Map,所以这个.

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

变成这样.

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

解决方案

`.split()` 将数据拆分为 java.util.map.使用 Jackson 或 Gson 将拆分后的数据编组以将其转换为 JSON.我遇到了同样的问题,并添加了 `marshal()` 解决了这个问题..marshal().json(JsonLibrary.Jackson)

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

解决方案

`.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)

这篇关于Apache Camel 拆分 JSONArray 去除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆