SpringBoot项目:pom.xml添加依赖;
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>
导包 : 切记不要导错包,
错误包 : import com.alibaba.fastjson.JSONObject;
正确的包如下:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
Json字符串格式化 : www.bejson.com
网页版:Json字符串格式化链接
案例如下:直接上代码:
package com.itheima;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/*
* 解析JSON字符串,json-->Object
* 1.确定需要解析的JSON是对象还是数组;
* 2.如果是对象,则使用JSONObject类;
* 3.如果是数组,则使用JSONArray类;
*
*/
public class JsonTest {
public static void main(String[] args) {
//对象格式的json字符串
String personStr = "{\"name\":\"小红\",\"age\":18,\"address\":\"西安\"}";
//对象格式的json字符串
String fruits = "{\"苹果\",\"香蕉\",\"橘子\"}";
//复杂的json格式
String p = "{\"name\":\"小赖\",\"age\":18,\"books\":[{\"bookName\":\"Java从入门\",\"price\":29.90},{\"bookName\":\"MySQL入门\",\"price\":39.90}]}";
//解析json对象字符串
JSONObject jsonObject = JSONObject.fromObject(personStr);
String name = jsonObject.getString("name");
System.out.println(name);
int age = jsonObject.getInt("age");
System.out.println(age);
String address = jsonObject.getString("address");
System.out.println(address);
//解析json数组字符串;
JSONArray jsonArray = JSONArray.fromObject(fruits);
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println()jsonArray.getString(i);
}
//复杂json解析方式一:
//解析复杂的json字符串;-->Person(nane,age,List<Book>)
Person person = new Person();
JSONObject json = JSONObject.fromObject(p);
String name = json.getString("name");
int age = json.getInt("age");
person.setName(name);
person.setAge(age);
//解析图书
List<Book> books = new ArrayList<>();
JSONArray jsonArray = jsonObject.getJSONArray("books");
for (int j = 0; j < jsonArray.size(); j++) {
Book book = new Book();
JSONObject bookObject = jsonArray.getJSONObject(j);
String bookName = bookObject.getString("bookName");
double price = bookObject.getDouble("price");
book.setBookName(bookName);
book.setPrice(price);
//添加集合
books.add(book);
}
person.setBooks(books);
System.out.println(person);
//复杂json解析方式二:反射实现;
JSONObject json = JSONObject.fromObject(p);
Person person = (Person)JSONObject.toBean(json,Person.class);
System.out.println(person);
String p2 = "[{\"name\":\"小婷\",\"age\":24},{\"name\":\"小桐\",\"age\":26}]";
List<Person> persons = (List<Person>)JSONArray.toCollection(JSONArray.fromObject(p2),Person.class);
System.out.println(persons);
}
}
解析过程需创建的对象:Person
package com.itheima;
import java.util.List;
public class Person {
private String name;
private int age;
private List<Book> books;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, List<Book> books) {
super();
this.name = name;
this.age = age;
this.books = books;
}
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 List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", books=" + books + "]";
}
}
解析过程需创建的对象:Book
package com.itheima;
public class Book {
private String bookName;
private double price;
public Book() {
super();
}
public Book(String bookName, double price) {
super();
this.bookName = bookName;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [bookName=" + bookName + ", price=" + price + "]";
}
}