将JSON反序列化为字符串的Map/List

5

我希望将Json反序列化为一个字符串的Map/List (例如 Map>...)

以下是我的输入:

{"pointsOfSale": 
{"pointOfSale":[ 
{"href":"\/pointsOfSale\/UUID.0abc2aca-7930-4c9e-9f38-8af3d0692e04", 
"model":{"href":"\/models\/modelePointOfSale", 
"modelType":{"href":"\/modelTypes\/pointOfSale"}}, 
"source":{"href":"\/sources\/TEST"}, 
"attribute":[ 
{"code":"pointOfSalePhysical","value":true}, 
{"code":"mail","value":"Mail1"}, 
{"code":"adresse","value":"address1"}]}, 
{"href":"\/pointsOfSale\/UUID.a12e7adf-652a-4197-91bf-d4785e43f09f", 
"model":{"href":"\/models\/modelePointOfSale", 
"modelType":{"href":"\/modelTypes\/pointOfSale"}}, 
"source":{"href":"\/sources\/Wikeo"}, 
"attribute":[ 
{"code":"pointOfSalePhysical","value":false}, 
{"code":"mail","value":"Mail1"}, 
{"code":"adresseMateriau","value":"Material address1"}]} 
}}

我希望在反序列化后能够像这样做“某事”:
myJsonMapped.get("pointsOfSale").get("pointOfSale").get(0).get("source").get("href").equals("\/sources\/TEST") == true 

例如,使用Gson我们可以进行这种解码:
new Gson().fromJson(json, Map.class); 

我知道我可以用简单的bean或processor等方式实现这个功能...

我只是想知道是否可以通过本地JSON camel组件配置更有效地完成此操作

编辑:我已经尝试了不同的方法,例如: unmarshal().json()... 或者 unmarshal().json(JsonLibrary.Gson, Map.class).. 等等... 但是没有成功:'(

1个回答

3
你可以使用jackson来实现这样的功能。
<dataFormats>
  <json id="jack" library="Jackson"/>
</dataFormats>

...

<route>
  <from uri="direct:test"/>
  <unmarshal ref="jack"/>
  <process ref="something"/>
</route>

或者使用Java和Gson:

 from("foo:bar")
    .unmarshal().json(JsonLibrary.Gson,Map.class)
    .to("foo:baz");

如果您无法让它正常工作,请说明错误等问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接