java怎么樣將字符串轉(zhuǎn)換成json數(shù)組對(duì)象?
1、需要json的相關(guān)jar包
2、設(shè)置json格式字符串:
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
3、建立相應(yīng)的類:
public class StudentList {
List<Student> student;
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
}
public class Student {
private String name;
private int age;
//private StudentClass studentClass;
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;
}
}
4、測(cè)試程序
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;