寻找一种简单的方法来解析JSON [英] Looking for a Straightforward Way to Parse JSON
本文介绍了寻找一种简单的方法来解析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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文