寻找一种简单的方法来解析JSON

编程入门 行业动态 更新时间:2024-10-12 05:44:30
本文介绍了寻找一种简单的方法来解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Java解析以下JSON:

{ "student_id": "123456789", "student_name": "Bart Simpson", "student_absences": 1}

完成此操作的最简单方法是什么.我尝试通过以下方式进行操作,但认为必须有一种更简单的方法.

import org.json.* JSONObject obj = new JSONArray("report"); for(int i = 0; I < arr.length(); i++){ String studentname = arr.getJSONObject(i).getString("student_id"); arr.getJSONObject(i).getString("student_name"); arr.getJSONObject(i).getString("student_name"); }

解决方案

有 Gson :

import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; public class Main { public static void main(String[] args) { String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}"; Student student = new Gson().fromJson(json, Student.class); System.out.println(student); } } class Student { @SerializedName("student_id") String studentId; @SerializedName("student_name") String studentName; @SerializedName("student_absences") Integer studentAbsences; @Override public String toString() { return "Student{" + "studentId='" + studentId + '\'' + ", studentName='" + studentName + '\'' + ", studentAbsences=" + studentAbsences + '}'; } }

另一个受欢迎的是杰克逊:

import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) throws Exception { String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}"; Student student = new ObjectMapper().readValue(json, Student.class); System.out.println(student); } } class Student { @JsonProperty("student_id") String studentId; @JsonProperty("student_name") String studentName; @JsonProperty("student_absences") Integer studentAbsences; @Override public String toString() { return "Student{" + "studentId='" + studentId + '\'' + ", studentName='" + studentName + '\'' + ", studentAbsences=" + studentAbsences + '}'; } }

在两种情况下,运行Main都会打印:

Student{studentId='123456789', studentName='Bart Simpson', studentAbsences=1}

编辑

并且无需创建Student类,您可以尝试使用 JsonPath 之类的东西. >

I am attempting to parse the following JSON using Java:

{ "student_id": "123456789", "student_name": "Bart Simpson", "student_absences": 1}

What is the simplest way to accomplish this. I tried doing it the way below but think there must be an easier way.

import org.json.* JSONObject obj = new JSONArray("report"); for(int i = 0; I < arr.length(); i++){ String studentname = arr.getJSONObject(i).getString("student_id"); arr.getJSONObject(i).getString("student_name"); arr.getJSONObject(i).getString("student_name"); }

解决方案

There's Gson:

import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; public class Main { public static void main(String[] args) { String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}"; Student student = new Gson().fromJson(json, Student.class); System.out.println(student); } } class Student { @SerializedName("student_id") String studentId; @SerializedName("student_name") String studentName; @SerializedName("student_absences") Integer studentAbsences; @Override public String toString() { return "Student{" + "studentId='" + studentId + '\'' + ", studentName='" + studentName + '\'' + ", studentAbsences=" + studentAbsences + '}'; } }

Another popular one is Jackson:

import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) throws Exception { String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}"; Student student = new ObjectMapper().readValue(json, Student.class); System.out.println(student); } } class Student { @JsonProperty("student_id") String studentId; @JsonProperty("student_name") String studentName; @JsonProperty("student_absences") Integer studentAbsences; @Override public String toString() { return "Student{" + "studentId='" + studentId + '\'' + ", studentName='" + studentName + '\'' + ", studentAbsences=" + studentAbsences + '}'; } }

In both cases, running Main will print:

Student{studentId='123456789', studentName='Bart Simpson', studentAbsences=1}

EDIT

And without creating a Student class, you could give something like JsonPath a try.

更多推荐

寻找一种简单的方法来解析JSON

本文发布于:2023-06-03 06:57:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法来   简单   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!