Unmarshal JSON to Map/List of Strings

与世无争的帅哥 提交于 2019-12-07 09:42:34

问题


I would like to unmarshal a Json to a Map/List of Strings (eg Map>...)

Here is my input:

{"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 

For instance, with Gson we can do this kind of decoding:

new Gson().fromJson(json, Map.class); 

I know I can do this with a simple bean or processor etc...

I just want to know of I can do this more efficiently with a native JSON camel component config

EDIT: I tried different thing already like : unmarshal().json()... or unmarshal().json(JsonLibrary.Gson, Map.class).. etc... without succes :'(


回答1:


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>

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.



来源:https://stackoverflow.com/questions/13085921/unmarshal-json-to-map-list-of-strings

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