java中解析一个json串,本文详细介绍了安卓原生的解析以及用fastjson解析

时间:2021-11-27 22:35:30

在java或安卓中解析JSON有很多种方式,可以用谷歌给安卓提供的原生的解析方式解析安卓,也可以选择第三方类库,一些比较流行的解析方式如fastjson,Gson等,本文就简单介绍一下java工程中fastjson解析json数据。在介绍fastjson解析之前,我想先给大家用谷歌给安卓提供的原生解析方式解析一个JSON数据串。

(一)google原生的解析方式:

用原生的解析方式需要用到一些google提供的类库,在你的工程下新建一个org.json包,然后可以从https://github.com/stleary/JSON-java下载其提供的java解析JSON的类库

然后把这些下载好的类复制到org.json包下,如下图:

java中解析一个json串,本文详细介绍了安卓原生的解析以及用fastjson解析

给出json串如下:

{"firstName":"John","lastName":"Smith","address":{"streetAddress":"奥运村西",
"city":"北京","postalCode":"100019","state":"北京"},
"phoneNumber":[{"number":" 10086","type":"home"},{"number":" 10010","type":"fax"}],"sex":"male","age":25}
{...}里面的是一个JSONObject,而[...]是一个JSONArray

由JSON字符串我们可以定义出实体类如下:

public class User {
private String firstName;
private String lastName;
private String sex;
private int age;
private Address address;
private List<Phone> phoneNumber;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public List<Phone> getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(List<Phone> phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
public class Address {
private String streetAddress;
private String city;
private String state;
private String postalCode;

public String getStreetAddress() {
return streetAddress;
}

public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getPostalCode() {
return postalCode;
}

public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
public class Phone {
private String type;
private String number;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}
}
将json字符串解析到实体类里面

public class TestJson02 {
public static void main(String[] args) {
String str = "{\"firstName\":\"John\",\"lastName\":\"Smith\",\"address\":{\"streetAddress\":\"奥运村西\",\"city\":\"北京\",\"postalCode\":\"100019\",\"state\":\"北京\"},\"phoneNumber\":[{\"number\":\" 10086\",\"type\":\"home\"},{\"number\":\" 10010\",\"type\":\"fax\"}],\"sex\":\"male\",\"age\":25}\n";
User user = parserUser(str);
System.out.println(toJSONString(user));
}
private static String toJSONString(User user) {
JSONObject userObj = new JSONObject();
userObj.put("firstName", user.getFirstName());
userObj.put("lastName", user.getLastName());
userObj.put("sex", user.getSex());
userObj.put("age", user.getAge());
JSONObject addressObj = new JSONObject();
Address address = user.getAddress();
addressObj.put("streetAddress", address.getStreetAddress());
addressObj.put("city", address.getCity());
addressObj.put("state", address.getState());
addressObj.put("postalCode", address.getPostalCode());
userObj.put("address", addressObj);
JSONArray phoneNumber = new JSONArray();
List<Phone> phones = user.getPhoneNumber();
for (Phone phone : phones) {
JSONObject number = new JSONObject();
number.put("type", phone.getType());
number.put("number", phone.getNumber());
phoneNumber.put(number);
}
userObj.put("phoneNumber", phoneNumber);
return userObj.toString();
}
private static User parserUser(String str) {
JSONObject object = new JSONObject(str);
User user = new User();
user.setFirstName(object.optString("firstName"));
user.setLastName(object.optString("lastName"));
user.setSex(object.optString("sex"));
user.setAge(object.optInt("age"));
JSONObject addressObj = object.optJSONObject("address");
Address address = new Address();
address.setStreetAddress(addressObj.optString("streetAddress"));
address.setCity(addressObj.optString("city"));
address.setState(addressObj.optString("state"));
address.setPostalCode(addressObj.optString("postalCode"));
user.setAddress(address);
List<Phone> phones = new ArrayList<>();
JSONArray phoneNumber = object.optJSONArray("phoneNumber");
for (int i = 0; i < phoneNumber.length(); i++) {
JSONObject numberObj = phoneNumber.optJSONObject(i);
Phone phone = new Phone();
phone.setType(numberObj.optString("type"));
phone.setNumber(numberObj.optString("number"));
phones.add(phone);
}
user.setPhoneNumber(phones);
return user;
}
}
(二)用fastjson去解析一个json字符串

用fastjson解析json字符串需要先导入jar包,在这里我推荐用maven去导入包,这样有一个好处,就是可以导入其依赖的相关jar包。

实体类如下图:

public class User {
@JSONField(name = "firstName")//注意,属性加入此注解,则在java文件混淆后依然可以得到这个属性
private String firstName;
private String lastName;
private String sex;
private int age;
@JSONField(serialize = false)//此注解可以使序列化后的json字符串中没有这个属性
private Address address;
private List<Phone> phoneNumber;
@JSONField(name = "10000")
private String text;
private int x;
private int y;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public List<Phone> getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(List<Phone> phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
@JSONField(name = "location")
public void setLocation(List<Integer> location) {
x = location.get(0);
y = location.get(1);
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
public class Address {
private String streetAddress;
private String city;
private String state;
private String postalCode;

public String getStreetAddress() {
return streetAddress;
}

public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getPostalCode() {
return postalCode;
}

public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
public class Phone {
private String type;
private String number;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}
}
解析方式如下代码:

public class Test {
public static void main(String[] args) {
String src = "{\n" +
" \"firstName\": \"John\",\n" +
" \"10000\": \"一万\",\n" +
" \"location\": [50, 100],\n" +
" \"lastName\": \"Smith\",\n" +
" \"sex\": \"male\",\n" +
" \"age\": 25,\n" +
" \"address\": \n" +
" {\n" +
" \"streetAddress\": \"21 2nd Street\",\n" +
" \"city\": \"New York\",\n" +
" \"state\": \"NY\",\n" +
" \"postalCode\": \"10021\"\n" +
" },\n" +
" \"phoneNumber\": \n" +
" [\n" +
" {\n" +
" \"type\": \"home\",\n" +
" \"number\": \"212 555-1234\"\n" +
" },\n" +
" {\n" +
" \"type\": \"fax\",\n" +
" \"number\": \"646 555-4567\"\n" +
" }\n" +
" ]\n" +
" }";
// Map parse = (Map) JSON.parse(src);
// System.out.println(((Map) parse.get("address")).get("streetAddress"));
// System.out.println(((Map) ((List) parse.get("phoneNumber")).get(0)).get("type"));
User user = JSON.parseObject(src, User.class);
System.out.println(user.getFirstName());
// System.out.println(user.getText());
// System.out.println(user.getX());
// System.out.println(user.getY());
System.out.println(JSON.toJSONString(user));
// List<Integer> list = JSON.parseArray("[100, 40, 12, 452]", Integer.class);
// System.out.println(user.getAddress().getCity());
// System.out.println(user.getPhoneNumber().get(0).getNumber());
}
}