how to convert JSONArray to List of Object using camel-jackson

后端 未结 5 1767
Happy的楠姐
Happy的楠姐 2020-12-23 09:51

Am having the String of json array as follow

{\"Compemployes\":[
    {
        \"id\":1001,
        \"name\":\"jhon\"
        },
        {
                \"         


        
5条回答
  •  时光取名叫无心
    2020-12-23 10:34

    /*
     It has been answered in http://stackoverflow.com/questions/15609306/convert-string-to-json-array/33292260#33292260
     * put string into file jsonFileArr.json
     * [{"username":"Hello","email":"hello@email.com","credits"
     * :"100","twitter_username":""},
     * {"username":"Goodbye","email":"goodbye@email.com"
     * ,"credits":"0","twitter_username":""},
     * {"username":"mlsilva","email":"mlsilva@email.com"
     * ,"credits":"524","twitter_username":""},
     * {"username":"fsouza","email":"fsouza@email.com"
     * ,"credits":"1052","twitter_username":""}]
     */
    
    public class TestaGsonLista {
    
    public static void main(String[] args) {
    Gson gson = new Gson();
     try {
        BufferedReader br = new BufferedReader(new FileReader(
                "C:\\Temp\\jsonFileArr.json"));
        JsonArray jsonArray = new JsonParser().parse(br).getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonElement str = jsonArray.get(i);
            Usuario obj = gson.fromJson(str, Usuario.class);
            //use the add method from the list and returns it.
            System.out.println(obj);
            System.out.println(str);
            System.out.println("-------");
        }
     } catch (IOException e) {
        e.printStackTrace();
     }
    }
    

提交回复
热议问题