JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性。近几年来已经和XML一样成为C/S架构中广泛采用的数据格式。有关JSON的更多知识,请参考以下内容:http://json.org/json-zh.html
在服务器和客户端之间使用JSON数据格式进行通信,经常会涉及到JAVA对象和JSON字符串之间的转换。通常,我们可以使用一些JSON解析工具,例如:Gson,FastJson等。当然,我们也可以手动解析,只是会比较繁琐。
下面通过一个实例来介绍使用Gson来进行JAVA嵌套对象和JSON字符串之间的相互转换。
MainActivity
[java] view plaincopy
- package com.example.jsonsample;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.widget.TextView;
- import com.example.jsonsample.data.Student;
- import com.example.jsonsample.data.Subject;
- import com.google.gson.Gson;
- public class MainActivity extends Activity {
- private TextView mTextView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mTextView = (TextView)findViewById(R.id.MyTextView);
- Gson gson = new Gson();
- //创建一个学生对象
- Student originStudent = getStudent();
- //将学生对象转换成JSON串
- String reponse = gson.toJson(originStudent);
- //将JSON串再还原成一个学生对象
- Student newstudent = gson.fromJson(reponse, Student.class);
- mTextView.setText(reponse);
- }
- public Student getStudent() {
- Subject sub1 = new Subject();
- sub1.setSubject_name("语文");
- sub1.setTeacher_name("张老师");
- Subject sub2 = new Subject();
- sub2.setSubject_name("数学");
- sub2.setTeacher_name("黄老师");
- Subject sub3 = new Subject();
- sub3.setSubject_name("英文");
- sub3.setTeacher_name("林老师");
- ArrayList<Subject> subjects = new ArrayList<Subject>();
- subjects.add(sub1);
- subjects.add(sub2);
- subjects.add(sub3);
- Student student = new Student();
- student.setName("杨辉");
- student.setSubjects(subjects);
- return student;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
Student
[java] view plaincopy
- package com.example.jsonsample.data;
- import java.io.Serializable;
- import java.util.ArrayList;
- /**
- * 学生类,包含学生名字和学科列表
- *
- * @author yanghui<[email protected]>
- */
- public class Student implements Serializable {
- /**
- * Serializable
- */
- private static final long serialVersionUID = -2689979321936117293L;
- private String name;
- private ArrayList<Subject> subjects;
- /**
- *
- * @return name 学生名字
- */
- public String getName() {
- return name;
- }
- /**
- *
- * @param name 学生名字
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- *
- * @return subjects 学科列表
- */
- public ArrayList<Subject> getSubjects() {
- return subjects;
- }
- /**
- *
- * @param subjects 学科列表
- */
- public void setSubjects(ArrayList<Subject> subjects) {
- this.subjects = subjects;
- }
- }
Subject
[java] view plaincopy
- package com.example.jsonsample.data;
- import java.io.Serializable;
- /**
- * 学科类,包含学科名字和学科老师名字
- *
- * @author yanghui<[email protected]>
- */
- public class Subject implements Serializable{
- /**
- * serialVersionUID
- */
- private static final long serialVersionUID = -2574980011831897251L;
- private String subject_name;
- private String teacher_name;
- /**
- *
- * @return subject_name 学科名称
- */
- public String getSubject_name() {
- return subject_name;
- }
- /**
- * @param subject_name 学科名称
- */
- public void setSubject_name(String subject_name) {
- this.subject_name = subject_name;
- }
- /**
- *
- * @return teacher_name 学科老师的名字
- */
- public String getTeacher_name() {
- return teacher_name;
- }
- /**
- *
- * @param teacher_name 学科老师的名字
- */
- public void setTeacher_name(String teacher_name) {
- this.teacher_name = teacher_name;
- }
- }
图片预览:
时间: 2024-10-19 17:17:25