如何使用Gson将嵌套的json对象添加到文件中?

时间:2014-07-15 23:46:38

标签: java android json gson

所以我有以下代码:

  Team team = new Team();
                        team.setLeader(player.getUniqueId().toString());
                        team.setTeamName(args[1]);
                        team.setSerilizedInventory(InventoryStringDeSerializer.InventoryToString(inventory));

                        List<String> mods = new ArrayList<String>();

                        team.setMods(mods);
                        team.setMembers(members);
                        Teams teams = new Teams();
                        teams.setTeam(team);
                        try{
                            Gson gson = new GsonBuilder().setPrettyPrinting().create();
                            Writer writer = new FileWriter(instance.getFile());
                            gson.toJson(team, writer);
                            writer.close();

                        }catch(IOException e){
                            e.printStackTrace();
                        }

我正在尝试创建一个包含我所有团队值的json文件,但我不确定如何在嵌套对象论坛中创建它。 我想跟随这个json structer。

{
    "teams",{
        "teamname",{
            "members":["069dcc56-cc5b-3867-a4cd-8c00ca516344"],
            "leader": "RenegadeEagle",
            "serilizedInventory": "54;",
            "mods":[],
            leader: "069dcc56-cc5b-3867-a4cd-8c00ca516344",
        }   
    }
}

这甚至可能不适合Json,但你明白了。那么我如何使用gson将其写入文件? 谢谢。

1 个答案:

答案 0 :(得分:0)

请检查此解决方案,您需要在多个级别提供参考

要将文件写入Android文件系统,请参阅此链接以获取解决方案 Error: Could not find or load main class com.example.pdfone.MainActivity

但是我强烈建议您将此JSON存储在SQL Lite表中。

Team.java

import java.util.List;


public class Team {

    private String leader;

    private String serilizedInventory;

    private List members;

    private List mods;

    public String getLeader() {
        return leader;
    }

    public void setLeader(String leader) {
        this.leader = leader;
    }

    public String getSerilizedInventory() {
        return serilizedInventory;
    }

    public void setSerilizedInventory(String serilizedInventory) {
        this.serilizedInventory = serilizedInventory;
    }

    public List getMembers() {
        return members;
    }

    public void setMembers(List members) {
        this.members = members;
    }

    public List getMods() {
        return mods;
    }

    public void setMods(List mods) {
        this.mods = mods;
    }



}

TeamsWrapper.java

import java.util.List;


public class TeamsWrapper {

    private List<Team> team;

    private String teamName;

    public List<Team> getTeam() {
        return team;
    }

    public void setTeam(List<Team> team) {
        this.team = team;
    }

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }



}

Test.java - &gt;主程序

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


public class Test {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Team team = new Team();

        team.setLeader("RenegadeEagle");
        team.setSerilizedInventory("54;");
        team.setMods(new ArrayList());


        List members = new ArrayList();
        members.add("069dcc56-cc5b-3867-a4cd-8c00ca516344");

        team.setMembers(members);

        TeamsWrapper tw = new TeamsWrapper();

        List ls1 = new ArrayList();

        ls1.add(team);
        ls1.add(team);

        tw.setTeam(ls1);
        tw.setTeamName("teamname");


        Gson gson = new GsonBuilder().create();
        System.out.println(gson.toJson(tw));



    }

}

程序的最终输出

{
    "team" : [{
            "leader" : "RenegadeEagle",
            "serilizedInventory" : "54;",
            "members" : ["069dcc56-cc5b-3867-a4cd-8c00ca516344"],
            "mods" : []
        }, {
            "leader" : "RenegadeEagle",
            "serilizedInventory" : "54;",
            "members" : ["069dcc56-cc5b-3867-a4cd-8c00ca516344"],
            "mods" : []
        }
    ],
    "teamName" : "teamname"
}