1. 程式人生 > 其它 >go 字串反序列化成物件陣列_使用java將json檔案反序列化成java物件

go 字串反序列化成物件陣列_使用java將json檔案反序列化成java物件

技術標籤:go 字串反序列化成物件陣列

原始碼如下:

package test;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStreamReader;import net.sf.json.JSON;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import net.sf.json.JSONSerializer;public class testJson {/** * @param args */public static void main(String[] args) {String path = "C:甥敳獲i042416Desktop1.txt";File file = new File(path);StringBuffer buffer = new StringBuffer();InputStreamReader read;try {read = new InputStreamReader( new FileInputStream(file));BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine() ) != null){ buffer.append(lineTxt);} read.close(); } catch (Exception e) {e.printStackTrace();}System.out.println("content: " + buffer.toString());JSON json = JSONSerializer.toJSON(buffer.toString()); JSONObject jsonObject = JSONObject.fromObject(json);JSONArray array = jsonObject.getJSONArray("statuses");int size = array.size();System.out.println("total post number: " + size);for( int i = 0; i < size; i++){JSONObject post = array.getJSONObject(i);System.out.println("****************************************************");System.out.println("Post Index: " + i);String id = post.getString("idstr");System.out.println("Post ID: " + id);System.out.println("Post content: " + post.getString("text"));System.out.println("Created at: " + post.getString("created_at"));JSONObject user = array.getJSONObject(i).getJSONObject("user");System.out.println("user ID: " + user.getString("idstr"));System.out.println("name: " + user.getString("name"));}}}
e1993821f1bf2e006b0c54debdfcacc1.png