如何解决在MyObject中解析JSON的问题?

huangapple go评论75阅读模式
英文:

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&lt;T&gt; {
private Gson gson=new Gson();

public JsonUtil(){}
public List&lt;T&gt; getJsonsFile(File file) throws IOException{
    List&lt;T&gt; list=null;
    try(Reader reader=new FileReader(file.getPath())) {
         list= gson.fromJson(reader, new TypeToken&lt;Collection&lt;T&gt;&gt;(){}.getType());

        return list;
    }
}
public void writeJsonInFile(File file,List&lt;T&gt; 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(&quot;MyFollowers copia.json&quot;);
JsonUtil&lt;Prova&gt; jsonUtil=new JsonUtil&lt;&gt;();
List&lt;Prova&gt; a=null;

try{
    a=jsonUtil.getJsonsFile(file);
}catch (IOException e){}
System.out.println(a.get(0));

}

My example of Json

[{&quot;id&quot;:&quot;alexandra04.___&quot;},{&quot;id&quot;:&quot;riky_vicini&quot;},{&quot;id&quot;:&quot;martatofanarii&quot;},{&quot;id&quot;:&quot;about._mary&quot;}]

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 &lt;T&gt; List&lt;T&gt; getJsonsFile(File file,Class&lt;T&gt; cls) throws IOException{
    List&lt;T&gt; list=new ArrayList&lt;T&gt;();
    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}]

huangapple
  • 本文由 发表于 2020年4月10日 02:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定