002.JDK方式——JSONObjectJSONArray

Posted wx62cfec15551e8

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了002.JDK方式——JSONObjectJSONArray相关的知识,希望对你有一定的参考价值。

002.JDK方式——JSONObject、JSONArray_ide

 

 

package com.jkd.entity;

public class Computer

public String cpu;
public String disk;

public String getCpu()

return cpu;


public void setCpu(String cpu)

this.cpu = cpu;


public String getDisk()

return disk;


public void setDisk(String disk)

this.disk = disk;


@Override
public String toString()

return "Computer" + "cpu=" + cpu + \\ + ", disk=" + disk + \\ + ;

package com.jkd.entity;

import java.util.ArrayList;

public class School

public String name;
public Computer computer;
public ArrayList<Student> students;

public String getName()

return name;


public void setName(String name)

this.name = name;


public Computer getComputer()

return computer;


public void setComputer(Computer computer)

this.computer = computer;


public ArrayList<Student> getStudents()

return students;


public void setStudents(ArrayList<Student> students)

this.students = students;


@Override
public String toString()

return "School" + "name=" + name + \\ + ", computer=" + computer + ", students=" + students + ;

package com.jkd.entity;

public class Student

public String name;
public int age;
public String sex;

public String getName()

return name;


public void setName(String name)

this.name = name;


public int getAge()

return age;


public void setAge(int age)

this.age = age;


public String getSex()

return sex;


public void setSex(String sex)

this.sex = sex;


@Override
public String toString()

return "Student" + "name=" + name + \\ + ", age=" + age + ", sex=" + sex + \\ + ;

package com.jdk.json;

import com.jkd.entity.Computer;
import com.jkd.entity.School;
import com.jkd.entity.Student;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class JsonA

public static String jsonString = "\\n" +
" \\"name\\": \\"teacher\\",\\n" +
" \\"computer\\": \\n" +
" \\"CPU\\": \\"intel7\\",\\n" +
" \\"disk\\": \\"512G\\"\\n" +
" ,\\n" +
" \\"students\\": [\\n" +
" \\n" +
" \\"name\\": \\"张三\\",\\n" +
" \\"age\\": 18,\\n" +
" \\"sex\\": \\"男\\"\\n" +
" ,\\n" +
" \\n" +
" \\"name\\": \\"李四\\",\\n" +
" \\"age\\": 19,\\n" +
" \\"sex\\": \\"男\\"\\n" +
" \\n" +
" ]\\n" +
"\\n";


public static void main(String[] args)

// 将Json转换为Java对象

School school=new School();
/**
* 1.导入包import org.json.JSONObject
*/
JSONObject jsonObject = new JSONObject(jsonString);
String name = (String) jsonObject.get("name");
school.setName(name);


JSONObject computerJsonObject = jsonObject.getJSONObject("computer");
Computer computer=new Computer();
String cpu = (String) computerJsonObject.get("CPU");
String disk = (String) computerJsonObject.get("disk");
computer.setCpu(cpu);
computer.setDisk(disk);

school.setComputer(computer);


ArrayList<Student> studentsList=new ArrayList<>();
JSONArray studentsJSONArray = jsonObject.getJSONArray("students");
for (int i=0;i<studentsJSONArray.length();i++)

JSONObject jsonObject1 = studentsJSONArray.getJSONObject(i);
String name1 = (String) jsonObject1.get("name");
int age = (int) jsonObject1.get("age");
String sex = (String) jsonObject1.get("sex");
Student student=new Student();
student.setName(name1);
student.setAge(age);
student.setSex(sex);
studentsList.add(student);

school.setStudents(studentsList);

System.out.println(school);
System.out.println("--------------------");
//java转json
String s = jsonObject.toString();
System.out.println(s);


 



以上是关于002.JDK方式——JSONObjectJSONArray的主要内容,如果未能解决你的问题,请参考以下文章

httpclient get/post请求

笔记之_Java的jsp页面常用

JSONObject类参数问题

net.sf.json.JSONObject 和org.json.JSONObject 的差别

java后台的json值怎么传给jsp页面,并进行遍历

json中怎么获取key