如何在java中使用json-simple返回单个对象?

时间:2017-12-22 12:29:56

标签: java json json-simple

我是这个json对象:

public static final String JSON_TEXT = "{" +
"\"Subjects\": [{" +
            "\"primaryKey\": \"1\"," +
            "\"subjectName\": \"English\"" +
        "}," +
        "{" +
            "\"primaryKey\": \"2\"," +
            "\"subjectName\": \"Spanish\"" +
        "}" +
    "]," +
    "\"Exams\": [{" +
            "\"primaryKey\": \"1\"," +
            "\"grade\": \"10\"" +
        "}," +
        "{" +
            "\"primaryKey\": \"2\"," +
            "\"grade\": \"20\"" +
        "}" +
    "]" +
"}";

需要为json dao实现构建一些方法。

我已经建成了

public Collection<Subject> getSubjects()

成功但我完全陷入了

public Subject findSubjectById(Integer subjectId)

这是我到目前为止的第一个想法:

public Subject findSubjectById(Integer subjectId) {
    JSONObject obj = (JSONObject)JSON_PARSER.parse(JSON_TEXT);
    if (obj.get(subjectId) != null)
        try {
            JSONArray subjectsArray = (JSONArray) obj.get("Subjects");

            for (int i = 0; i < subjectsArray.length(); i++){   

            }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
        return subject;

} 

任何想法/示例都非常感谢您解决此问题。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

在你的for循环中尝试这个

JSONObject subDtl=subjectsArray.getJSONObject(i);
int subKey=subDtl.getInt("primaryKey"); 
String subName=subDtl.getString("subjectName");

答案 1 :(得分:0)

我知道你不使用Gson但会解决你所有的问题。 另外,我建议将字段名称更改为“主题”和“考试”,而不是“主题”和“考试”。

这是至少值得考虑的完整解决方案。

public class Data {

    private List<Subject> subjects;

    private List<Exam> exams;

    public Data(String json) {
        Data data = new Gson().fromJson(json, Data.class);
        this.subjects = data.subjects;
        this.exams = data.exams;
        Reporter.log(new Gson().toJson(this), true);
    }

    public Subject findSubjectById(Integer id) {
        return subjects.stream().filter(subject -> subject.primaryKey.equals(id)).findAny().orElse(null);
    }

    public Exam findExamById(Integer id) {
        return exams.stream().filter(exam -> exam.primaryKey.equals(id)).findAny().orElse(null);
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }

    public List<Exam> getExams() {
        return exams;
    }

    public void setExams(List<Exam> exams) {
        this.exams = exams;
    }

    public class Subject {
        private Integer primaryKey;
        private String subjectName;

        public Integer getPrimaryKey() {
            return primaryKey;
        }

        public void setPrimaryKey(Integer primaryKey) {
            this.primaryKey = primaryKey;
        }

        public String getSubjectName() {
            return subjectName;
        }

        public void setSubjectName(String subjectName) {
            this.subjectName = subjectName;
        }
    }

    public class Exam {
        private Integer primaryKey;
        private String grade;

        public Integer getPrimaryKey() {
            return primaryKey;
        }

        public void setPrimaryKey(Integer primaryKey) {
            this.primaryKey = primaryKey;
        }

        public String getGrade() {
            return grade;
        }

        public void setGrade(String grade) {
            this.grade = grade;
        }
    }

}

以下是验证我们解决方案的测试

@SpringBootTest(classes = TestNGWithSpringApplication.class)
public class JsonDataIT extends AbstractTestNGSpringContextTests {

    String json = "{  \n"
            + "   \"subjects\":[  \n"
            + "      {  \n"
            + "         \"primaryKey\":\"1\",\n"
            + "         \"subjectName\":\"English\"\n"
            + "      },\n"
            + "      {  \n"
            + "         \"primaryKey\":\"2\",\n"
            + "         \"subjectName\":\"Spanish\"\n"
            + "      }\n"
            + "   ],\n"
            + "   \"exams\":[  \n"
            + "      {  \n"
            + "         \"primaryKey\":\"1\",\n"
            + "         \"grade\":\"10\"\n"
            + "      },\n"
            + "      {  \n"
            + "         \"primaryKey\":\"2\",\n"
            + "         \"grade\":\"20\"\n"
            + "      }\n"
            + "   ]\n"
            + "}";

    @Test
    public void findSubjectTest() {
        Data.Subject subject = new Data(json).findSubjectById(new Integer(2));
        Assert.assertEquals(subject.getSubjectName(), "Spanish");
    }

    @Test
    public void findExamTest() {
        Data.Exam exam = new Data(json).findExamById(new Integer(2));
        Assert.assertEquals(exam.getGrade(), "20");
    }

}

那应该有用