英文:
How resolve Problem with Parsing Json in MyObject?
问题
抱歉,我明白您只需要翻译文本内容,不需要额外的回答。以下是您提供的内容的翻译:
我对我的代码有一个大问题。我创建了一个用于序列化和反序列化Json对象的个人类,并且我使用了GSON,但是。
这是我的类。
    public class JsonUtil<T> {
    private Gson gson = new Gson();
    public JsonUtil() {}
    public List<T> getJsonsFile(File file) throws IOException {
        List<T> list = null;
        try (Reader reader = new FileReader(file.getPath())) {
            list = gson.fromJson(reader, new TypeToken<Collection<T>>(){}.getType());
            return list;
        }
    }
    public void writeJsonInFile(File file, List<T> list) throws IOException {
        try (Writer writer = new FileWriter(file.getPath())) {
            gson.toJson(list, writer);
        }
    }
    
这是我的主类
    public class Prova {
    public String id;
    public Prova(String a) {
        id = a;
    }
    public static void main(String[] args) {
        final File file = new File("MyFollowers copia.json");
        JsonUtil<Prova> jsonUtil = new JsonUtil<>();
        List<Prova> a = null;
        try {
            a = jsonUtil.getJsonsFile(file);
        } catch (IOException e) {}
        System.out.println(a.get(0));
    }
    
我的Json示例
    [{"id":"alexandra04.___"},{"id":"riky_vicini"},{"id":"martatofanarii"},{"id":"about._mary"}]
现在当我运行我的代码时,我得到了这个输出
    {id=alexandra04.___}
我查找了这些问题,但我不明白如何解决这个问题。
我想要我的对象 List<Prova> a 包含所有的 Prova 对象,并且我想要使用以下代码
    for (Prova prova : a) {
        System.out.println(a.id);
    }
嗨,我用以下代码解决了这个问题
    public <T> List<T> getJsonsFile(File file, Class<T> cls) throws IOException {
        List<T> list = new ArrayList<T>();
        try (Reader reader = new FileReader(file.getPath())) {
            JsonArray array = new JsonParser().parse(reader).getAsJsonArray();
            for (JsonElement jsonElement : array) {
                list.add(gson.fromJson(jsonElement, cls));
            }
            return list;
        }
    }
英文:
Hi i have a big problem with my code. I realize a personal class for serialize and deserialize objects in Json and for this i use GSON but.
This is my class.
public class JsonUtil<T> {
private Gson gson=new Gson();
public JsonUtil(){}
public List<T> getJsonsFile(File file) throws IOException{
    List<T> list=null;
    try(Reader reader=new FileReader(file.getPath())) {
         list= gson.fromJson(reader, new TypeToken<Collection<T>>(){}.getType());
        return list;
    }
}
public void writeJsonInFile(File file,List<T> list) throws IOException{
    try(Writer writer=new FileWriter(file.getPath())){
        gson.toJson(list, writer);
    }
}
And this is my main
public class Prova{
public String id;
public Prova(String a){
    id=a;
}
public static void main(String[] args){
final File file=new File("MyFollowers copia.json");
JsonUtil<Prova> jsonUtil=new JsonUtil<>();
List<Prova> a=null;
try{
    a=jsonUtil.getJsonsFile(file);
}catch (IOException e){}
System.out.println(a.get(0));
}
My example of Json
[{"id":"alexandra04.___"},{"id":"riky_vicini"},{"id":"martatofanarii"},{"id":"about._mary"}]
Now when i run my code i get this output
{id=alexandra04.___}
I search this problems but i don't understand how to resolve this
I'll want my object List<Prova> a with all objects Prova and i'll want use this
for(Prova prova:a){System.out.println(a.id);} 
HI I RESOLVED WITH THIS
public <T> List<T> getJsonsFile(File file,Class<T> cls) throws IOException{
    List<T> list=new ArrayList<T>();
    try(Reader reader=new FileReader(file.getPath())) {
        JsonArray arry=new JsonParser().parse(reader).getAsJsonArray();
        for(JsonElement jsonElement:arry){
            list.add(gson.fromJson(jsonElement,cls));
        }
    return list;
        //return list;
    }
}
答案1
得分: 2
你的代码运行得很好,你只需要更改代码中的最后一条指令,因为你在控制台显示的是列表中的第一个元素
 System.out.println(a.get(0)); // 将此指令更改为以下内容
 System.out.println(a);   // 显示结果为 [{id=alexandra04.___}, {id=riky_vicini}, {id=martatofanarii}, {id=about._mary}]
英文:
your code work perfectly you need to change last instruction in your code because you show in console first element in list
 System.out.println(a.get(0)); // change this instruction by this 
 System.out.println(a);   // show result is [{id=alexandra04.___}, {id=riky_vicini}, {id=martatofanarii}, {id=about._mary}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论