JSONArray資料轉換為實體類
TableA、TableB 是根據json資料建立的實體類 建議將json資料放到json.cn
裡面格式化
json-lib是一個java類庫,提供將Java物件,包括beans, maps, collections, java arrays and XML等轉換成JSON,或者反向轉換的功能。
import net.sf.json.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class findElectSet {
/*
* 取出檔案內容,填充物件
*/
public List<TableA> writeFile(String path) {
List<TableA> tableAS = new ArrayList<>();
String sets = ReadFile(path);//獲得json檔案的內容
JSONObject jsonObject = JSONObject.fromObject(sets);//格式化成json物件
Object data = jsonObject.get("data");
JSONObject jsonObject1 = JSONObject.fromObject(data);
String list = jsonObject1.getString("list");
JSONArray jsonArray = JSONArray.fromObject(list);
List<TableA> tableAList = new ArrayList<>();
for (Object o : jsonArray) {
JSONObject jo = JSONObject.fromObject(o);
TableA tableA = new TableA();
tableA.setId(jo.getString("id"));
tableA.setZbbmText(jo.getString("zbbmText"));
tableA.setCreateDate(jo.getString("createDate"));
tableA.setTopic(jo.getString("topic"));
tableA.setCreateUserText(jo.getString("createUserText"));
tableA.setWh(jo.getString("wh"));
tableA.setFwrq(jo.getString("fwrq"));
List<TableB> tableBList = new ArrayList<>();
String attlist = jo.getString("attlist");
JSONArray jsonArray1 = JSONArray.fromObject(attlist);
for (Object jsonArr1 : jsonArray1) {
JSONObject j = JSONObject.fromObject(jsonArr1);
TableB tableB = new TableB();
tableB.setContentId(j.getString("contentId"));
tableB.setDurl(j.getString("durl"));
tableB.setTopic(j.getString("topic"));
tableB.setType(j.getString("type"));
tableBList.add(tableB);
}
tableA.setAttlist(tableBList);
tableAList.add(tableA);
return tableAList;
//讀檔案,返回字串public String ReadFile(String path) {
StringBuffer strBuf = new StringBuffer();
try {
URL url = new URL(path);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = null;
while((line = reader.readLine()) != null) {
strBuf.append(line + " ");
reader.close();
} catch (MalformedURLException e) {
System.out.println();
} catch (IOException e) {
System.out.println(e);
return strBuf.toString();
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
findElectSet f=new findElectSet();
List<TableA> tableAS = f.writeFile("http://XXXXXXXXXXXXXXXXX");
System.out.println(tableAS.toString());
JSONArray資料轉換為實體類
TableA、TableB 是根據json資料建立的實體類 建議將json資料放到json.cn
裡面格式化
json-lib是一個java類庫,提供將Java物件,包括beans, maps, collections, java arrays and XML等轉換成JSON,或者反向轉換的功能。
import net.sf.json.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class findElectSet {
/*
* 取出檔案內容,填充物件
*/
public List<TableA> writeFile(String path) {
List<TableA> tableAS = new ArrayList<>();
String sets = ReadFile(path);//獲得json檔案的內容
JSONObject jsonObject = JSONObject.fromObject(sets);//格式化成json物件
Object data = jsonObject.get("data");
JSONObject jsonObject1 = JSONObject.fromObject(data);
String list = jsonObject1.getString("list");
JSONArray jsonArray = JSONArray.fromObject(list);
List<TableA> tableAList = new ArrayList<>();
for (Object o : jsonArray) {
JSONObject jo = JSONObject.fromObject(o);
TableA tableA = new TableA();
tableA.setId(jo.getString("id"));
tableA.setZbbmText(jo.getString("zbbmText"));
tableA.setCreateDate(jo.getString("createDate"));
tableA.setTopic(jo.getString("topic"));
tableA.setCreateUserText(jo.getString("createUserText"));
tableA.setWh(jo.getString("wh"));
tableA.setFwrq(jo.getString("fwrq"));
List<TableB> tableBList = new ArrayList<>();
String attlist = jo.getString("attlist");
JSONArray jsonArray1 = JSONArray.fromObject(attlist);
for (Object jsonArr1 : jsonArray1) {
JSONObject j = JSONObject.fromObject(jsonArr1);
TableB tableB = new TableB();
tableB.setContentId(j.getString("contentId"));
tableB.setDurl(j.getString("durl"));
tableB.setTopic(j.getString("topic"));
tableB.setType(j.getString("type"));
tableBList.add(tableB);
}
tableA.setAttlist(tableBList);
tableAList.add(tableA);
}
return tableAList;
}
//讀檔案,返回字串public String ReadFile(String path) {
StringBuffer strBuf = new StringBuffer();
try {
URL url = new URL(path);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = null;
while((line = reader.readLine()) != null) {
strBuf.append(line + " ");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println();
} catch (IOException e) {
System.out.println(e);
}
return strBuf.toString();
}
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
findElectSet f=new findElectSet();
List<TableA> tableAS = f.writeFile("http://XXXXXXXXXXXXXXXXX");
System.out.println(tableAS.toString());
}
}