当前位置: 首页 > 知识库问答 >
问题:

使用Json的对象列表

卢树
2023-03-14

我在Deck类中创建一个问题对象列表。我读取Json文件并将其传递给String,然后简单地使用Gson创建对象列表。但那不起作用:/你有谢谢你的主意吗。这是我的Json文件:

{
    "questions": [{
            "author": "Florian Genicq",
            "category": "NETWORK",
            "statement": "What stands for LAN?",
            "answer": "Local Area Network"
        },
        {
            "author": "Florian Genicq",
            "category": "NETWORK",
            "statement": "Which network requires terminators?",
            "answer": "BUS network"
        },
        {
            "author": "Florian Genicq",
            "category": "NETWORK",
            "statement": "Which network topology is fastest?",
            "answer": "Star network"
        },
        {
            "author": "Florian Genicq",
            "category": "NETWORK",
            "statement": "What is the maximum bandwith that can be supported by filter optics cable?(Mbps)",
            "answer": 2000
        },
        {
            "author": "Florian Genicq",
            "category": "Operating System",
            "statement": "Which operating system doesn’t support networking computers?",
            "answer": "Windows 3.1"
        },
        {
            "author": "Florian Genicq",
            "category": "Operating System",
            "statement": "Usuallu, in MSDOS, the primary hard disk drives has the drive letter ___",
            "answer": "C"
        },
        {
            "author": "Médéric Allard",
            "category": "Social Network",
            "statement": "How many characters can you use on Twitter?",
            "answer": 280
        },
        {
            "author": "Médéric Allard",
            "category": "Operating System",
            "statement": "How many characters can you use on Twitter?",
            "answer": 280

        }

    ]
}

这是我的问题构造器:

private String author,statement,answer;
private Category category;

public Question(String au,Category c,String s,String an) {
    this.author=au;
    this.category=c;
    this.statement=s;
    this.answer=an;

}

这是我的甲板建造师:

private List<Question> listQuestion;

public Deck() {
    listQuestion = new ArrayList<>();
}

这是我的主要代码:

Gson gson = new Gson();
String res1  = JsonTransform.readFromFile("/json.json", true);
//System.out.println(res1);
Deck res2 = gson.fromJson(res1, Deck.class);
System.out.println(res2);

res1是我以字符串格式获得的Json文件,因此res2应该包含一个与Json文件相关的对象列表,但列表中没有任何内容。

共有1个答案

程瑞
2023-03-14

我不确定JsonTransform类是什么,所以不能对此进行评论,但下面的代码可以正常工作。问题是您应该将deck类的listquestion变量名更改为questions或使用属性@serializedName(“questions”)进行注释;

下面是相同的完整代码。我已经使用BufferedReader从文件中读取json

课堂问题

import java.util.List;

/**
 * @author Utkarsh Thusoo
 * @version $Id: $
 */
public class Questions
{

    private String author;

    private String category;

    private String statement;

    private String answer;

    /**
     * @return the author
     */
    public String getAuthor()
    {
        return author;
    }

    /**
     * @param author
     *            the author to set
     */
    public void setAuthor(String author)
    {
        this.author = author;
    }

    /**
     * @return the category
     */
    public String getCategory()
    {
        return category;
    }

    /**
     * @param category
     *            the category to set
     */
    public void setCategory(String category)
    {
        this.category = category;
    }

    /**
     * @return the statement
     */
    public String getStatement()
    {
        return statement;
    }

    /**
     * @param statement
     *            the statement to set
     */
    public void setStatement(String statement)
    {
        this.statement = statement;
    }

    /**
     * @return the answer
     */
    public String getAnswer()
    {
        return answer;
    }

    /**
     * @param answer
     *            the answer to set
     */
    public void setAnswer(String answer)
    {
        this.answer = answer;
    }

    /** 
     * @inheritDoc
     */
    @Override
    public String toString()
    {
        return "Questions [author=" + author + ", category=" + category + ", statement=" + statement
                + ", answer=" + answer + "]";
    }



}

级甲板

/**
 * @author Utkarsh Thusoo
 * @version $Id: $
 */
public class Deck
{

    private List<Questions> questions;

    /**
     * @return the questions
     */
    public List<Questions> getQuestions()
    {
        return questions;
    }

    /**
     * @param questions the questions to set
     */
    public void setQuestions(List<Questions> questions)
    {
        this.questions = questions;
    }

    /** 
     * @inheritDoc
     */
    @Override
    public String toString()
    {
        return "Deck [questions=" + questions + "]";
    }


}

测试类

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;

public class Test
{

    public static void main(String[] args) throws IOException
    {
        Gson gson = new Gson();
        BufferedReader br = new BufferedReader(new FileReader("json.json"));
        StringBuffer res1 = new StringBuffer();

        String currLine = "";
        while((currLine = br.readLine())!=null){
            res1.append(currLine); 
        }


        Deck res2 = gson.fromJson(res1.toString(), Deck.class);
        System.out.println(res2);

    }

}

输出

Deck [questions=[Questions [author=Florian Genicq, category=NETWORK, statement=What stands for LAN?, answer=Local Area Network], Questions [author=Florian Genicq, category=NETWORK, statement=Which network requires terminators?, answer=BUS network], Questions [author=Florian Genicq, category=NETWORK, statement=Which network topology is fastest?, answer=Star network], Questions [author=Florian Genicq, category=NETWORK, statement=What is the maximum bandwith that can be supported by filter optics cable?(Mbps), answer=2000], Questions [author=Florian Genicq, category=Operating System, statement=Which operating system doesn’t support networking computers?, answer=Windows 3.1], Questions [author=Florian Genicq, category=Operating System, statement=Usuallu, in MSDOS, the primary hard disk drives has the drive letter ___, answer=C], Questions [author=Médéric Allard, category=Social Network, statement=How many characters can you use on Twitter?, answer=280], Questions [author=Médéric Allard, category=Operating System, statement=How many characters can you use on Twitter?, answer=280]]]
 类似资料:
  • 问题内容: 我有一个像这样的JSON对象: 并尝试与Gson解析: 但是“ responceBean”始终为“ null” 这是所有其他类: 这是我的最新尝试。我不知何故找不到正确的方法。任何帮助将不胜感激。 问题答案: 您的 JSON模型与您的对象模型不匹配 。 您需要一个中间层来填补空白: TypeAdapter 。 而且,没有用户的命名信息。 最后是名称不匹配:JSON中的“ worklog

  • 问题内容: 我有两个问题: 如何使用Spring RestTemplate映射JSON对象列表。 如何映射嵌套的JSON对象。 问题答案: 也许这样 控制器代码 RequestMapping 是对扩展名的添加状态代码。使用以及方法。在此类中,由和返回。

  • 问题内容: 我将实例转换为JSON时遇到问题: 在我有一个实例列表。 返回,例如: 但是我只想要1个JSON字符串,列表中包含所有信息: 问题答案: 您可以使用列表推导生成字典列表,然后将其转换为: 或使用功能;将调用它无法序列化的任何内容: 后者适用于在结构的任何级别插入的对象,而不仅仅是在列表中。 就个人而言,我将使用棉花糖之类的项目来处理更复杂的事情。例如处理您的示例数据可以用

  • 我有两个问题: 如何使用spring RestTemplate映射JSON对象列表。 如何映射嵌套的JSON对象。 我正在尝试使用https://bitpay.com/api/rates,方法是按照http://spring.io/guides/gs/consource-rest/的教程学习。

  • 我有json格式,我想用JSONObject和JSONArray在java代码中创建它,但是我没有得到正确格式的输出。JSON格式如下所示。 我如何用java创建这个json对象,因为我想用这个对象创建使用NVD3的多条形图。如有任何帮助,不胜感激!

  • 我目前正在开发一个Java web应用程序,它使用Magento REST API公开的JSON数据。api返回的数据示例如下: 我的应用程序中有一个Java类,如下所示: 我想对数据进行反序列化,并将其转换为,但我总是得到以下错误: 这是我用来将JSON响应反序列化为ArrayList的语句行: 有人能分享一些见解吗?我看到一些例子,返回的JSON对象前面没有任何ID。那是因为我做错了什么吗?非