gson将字符串转化为json数组注意事项

首先说说怎样把j对象数组转换为json字符串,用gson两句话搞定。

 Gson gson=new Gson();
        String friendlistresult=gson.toJson(friendList);

接下来,要把json字符串转为对象数组则稍微麻烦一些。

Gson gson=new Gson();
                List tempList=new ArrayList<>();
                tempList = gson.fromJson(spContactList.getString("friendList",""), new TypeToken>(){}.getType());
                for(int i=0;i

刚开始,我以为tempList可以直接当对象数组用,但发现用来构建listview没有数据,然后tempList.get(0). getFriendname ()又有值,所以我循环把tempList的对象数组取出来,放到FriendList中,结果数据果然回来了

你可能感兴趣的:(gson将字符串转化为json数组注意事项)