java list转json 和json 转list 笔记
2020-12-15 本文已影响0人
xq9527
java list集合转json
public static void getJson() throws JSONException {
List<Person> personList = new ArrayList<Person>();
for (int i = 0; i < 5; i++) {
Person person=new Person();
person.setName("xuiqing");
person.setAge(20);
person.setSex("男");
personList.add(person);
}
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
JSONObject tmpObj = null;
int count = personList.size();
for(int i = 0; i < count; i++) {
tmpObj = new JSONObject();
tmpObj.put("name" , personList.get(i).name);
tmpObj.put("sex", personList.get(i).sex);
tmpObj.put("age", personList.get(i).age);
jsonArray.put(tmpObj);
tmpObj = null;
}
String personInfos = jsonArray.toString(); // 将JSONArray转换得到String
jsonObject.put("personInfos" , personInfos); // 获得JSONObject的String
dataList(personInfos);
System.out.println("personInfos --- > " +personInfos );
}
json 数组转list集合
public static List<Person>dataList(String jsonstr){
List<Person>getData=new ArrayList<>();
if(jsonstr!=null&&jsonstr.length()>0){
try {
JSONArray jsonArray=new JSONArray(jsonstr);
JSONObject jsonObject=null;
for (int i = 0; i <jsonArray.length() ; i++) {
jsonObject=jsonArray.getJSONObject(i);
Person person=new Person();
person.setName(jsonObject.optString("name"));
person.setAge(jsonObject.optInt("age"));
person.setSex(jsonObject.optString("sex"));
getData.add(person);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return getData;
}
bean类
package com.example.demo;
public class Person {
public String name;
public String sex;
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}
完整代码:
package com.example.demo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
try {
getJson();
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void getJson() throws JSONException {
List<Person> personList = new ArrayList<Person>();
for (int i = 0; i < 5; i++) {
Person person=new Person();
person.setName("xuiqing");
person.setAge(20);
person.setSex("男");
personList.add(person);
}
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
JSONObject tmpObj = null;
int count = personList.size();
for(int i = 0; i < count; i++) {
tmpObj = new JSONObject();
tmpObj.put("name" , personList.get(i).name);
tmpObj.put("sex", personList.get(i).sex);
tmpObj.put("age", personList.get(i).age);
jsonArray.put(tmpObj);
tmpObj = null;
}
String personInfos = jsonArray.toString(); // 将JSONArray转换得到String
jsonObject.put("personInfos" , personInfos); // 获得JSONObject的String
dataList(personInfos);
System.out.println("personInfos --- > " +personInfos );
}
public static List<Person>dataList(String jsonstr){
List<Person>getData=new ArrayList<>();
if(jsonstr!=null&&jsonstr.length()>0){
try {
JSONArray jsonArray=new JSONArray(jsonstr);
JSONObject jsonObject=null;
for (int i = 0; i <jsonArray.length() ; i++) {
jsonObject=jsonArray.getJSONObject(i);
Person person=new Person();
person.setName(jsonObject.optString("name"));
person.setAge(jsonObject.optInt("age"));
person.setSex(jsonObject.optString("sex"));
getData.add(person);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return getData;
}
}