Java Gson数据存储和检索。存储为字符串,检索为整数

Java Gson数据存储和检索。存储为字符串,检索为整数,java,arrays,string,gson,parseint,Java,Arrays,String,Gson,Parseint,在我的datastorage类中,我使用Gson保存名为setTeam的字符串数据,并检索名为getTeam public class DataStorage { private static final String TEAM = "Teams"; private static final String TEAM_INFO = "teaminfo"; private static Gson gson = new Gson();

在我的datastorage类中,我使用Gson保存名为
setTeam
的字符串数据,并检索名为
getTeam

public class DataStorage {
        private static final String TEAM = "Teams";
        private static final String TEAM_INFO = "teaminfo";
        private static Gson gson = new Gson();


        public static void setTeam(MainActivity context, Collection<User> teams) {
            String usersJson = gson.toJson(teams);
            SharedPreferences.Editor editor = context.getSharedPreferences(TEAM_INFO, Context.MODE_PRIVATE).edit();
            editor.putString(TEAM, usersJson);
            editor.apply();
        }

        public static ArrayList<User> getTeam(Context context){
            SharedPreferences prefs = context.getSharedPreferences(TEAM_INFO, Context.MODE_PRIVATE);
            String usersJson = prefs.getString(TEAM, "[]");
            return gson.fromJson(usersJson, new TypeToken<ArrayList<User>>(){}.getType());
        }

    }
在主活动中,我创建了一个包含一些数字的双字符串
ArrayList

private final String[][] dataTeams = new String[30][3];
因此,在dataTeams double阵列中,有30个团队拥有他们的GameWin和gameLost。gameWon和gameLost是数字字符串

因此,现在我尝试将dataTeams中的任何内容保存到Json数据库中

User c = new User();
c.setName(dataTeams[i][0]);
c.setGameWon(Integer.parseInt(dataTeams[i][2]));
c.setGameLost(Integer.parseInt(dataTeams[i][3]));

teams.add(c);
DataStorage.setTeam(this, teams);
但是,当我检索数据时,我已经成功地完成了这项工作

ArrayList<User> users = DataStorage.getTeam(this);
        String [] teams = new String[30];
        for(int i = 0; i < 30; i++) {
            teams[i] = users.get(i).getName() + "  W:" + users.get(i).getGameWon() + " L:" + users.get(i).getGameLost();
        }
或者我解析的整数是错误的

dataTeam中的数据是

Team    W   L   
Atlanta     60  22  
Boston      40  42  
Brooklyn    38  44  
Charlotte   33  49
Chicago     50  32  
Cleveland   53  29  
Dallas      50  32  
Denver      30  52  
Detroit     32  50  
Golden State 67 15  
Houston     56  26  
Indiana     38  44  
L.A. Clippers   56  26  
L.A. Lakers     21  61  
Memphis     55  27  
Miami       37  45  
Milwaukee   41  41  
Minnesota   16  66  
New Orleans 45  37  
New York    17  65  
Oklahoma City   45  37  
Orlando     25  57  
Philadelphia    18  64  
Phoenix     39  43  
Portland    51  31  
Sacramento  29  53  
San Antonio 55  27  
Toronto     49  33  
Utah        38  44  
Washington  46  36  
你的声明:

private final String[][] dataTeams = new String[30][3];
您的设置数据逻辑:

c.setGameWon(Integer.parseInt(dataTeams[i][2]));  // << [i][1]
c.setGameLost(Integer.parseInt(dataTeams[i][3]));  // << [i][2]

c.setGameWon(Integer.parseInt(dataTeams[i][2]);//你能分享你的JSON数据吗?@PhilipRollins当然,我已经把它添加到底部了。我认为你的数据不是JSON,而是tsv(制表符分隔的值)。所以你可以使用一些CSV库来提高阅读效率。
private final String[][] dataTeams = new String[30][3];
c.setGameWon(Integer.parseInt(dataTeams[i][2]));  // << [i][1]
c.setGameLost(Integer.parseInt(dataTeams[i][3]));  // << [i][2]