将 JSON 解组为映射/字符串列表 [英] Unmarshal JSON to Map/List of Strings
问题描述
我想将一个 Json 解组为一个 Map/List of Strings(例如 Map>...)
I would like to unmarshal a Json to a Map/List of Strings (eg 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"}]}
}}
我希望能够在解组后做这样的事情":
I would like to be able to do "something" like this after unmarshaling:
myJsonMapped.get("pointsOfSale").get("pointOfSale").get(0).get("source").get("href").equals("/sources/TEST") == true
例如,使用 Gson 我们可以进行这种解码:
For instance, with Gson we can do this kind of decoding:
new Gson().fromJson(json, Map.class);
我知道我可以用一个简单的 bean 或处理器等来做到这一点......
I know I can do this with a simple bean or processor etc...
我只是想知道我可以使用原生 JSON 骆驼组件配置更有效地做到这一点
I just want to know of I can do this more efficiently with a native JSON camel component config
我已经尝试了不同的东西:解组().json()...或者unmarshal().json(JsonLibrary.Gson, Map.class)..等等...没有成功:'(
I tried different thing already like : unmarshal().json()... or unmarshal().json(JsonLibrary.Gson, Map.class).. etc... without succes :'(
推荐答案
你可以用杰克逊做这样的事情.
You could do something like this with jackson.
<dataFormats>
<json id="jack" library="Jackson"/>
</dataFormats>
...
<route>
<from uri="direct:test"/>
<unmarshal ref="jack"/>
<process ref="something"/>
</route>
或者在java中使用gson:
Or in java with gson:
from("foo:bar")
.unmarshal().json(JsonLibrary.Gson,Map.class)
.to("foo:baz");
如果你没有让它工作,请说明错误,等等.
If you're not getting it to work, please state error and so fourth.
这篇关于将 JSON 解组为映射/字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!