Gson讀寫JSON 資料

來源:互聯網
上載者:User

標籤:blog   io   ar   os   java   for   sp   資料   div   

一、建立實體;

package com.bo.entity;public class ShiTiInFo {private String shitia; //試題名稱private String shitib; //大題名稱private String shitic; //小題名稱private String shitid; //試題題號private int shitie; //試題小號public String getShitia() {return shitia;}public void setShitia(String shitia) {this.shitia = shitia;}public int getShitie() {return shitie;}public void setShitie(int shitie) {this.shitie = shitie;}public String getShitib() {return shitib;}public void setShitib(String shitib) {this.shitib = shitib;}public String getShitic() {return shitic;}public void setShitic(String shitic) {this.shitic = shitic;}public String getShitid() {return shitid;}public void setShitid(String shitid) {this.shitid = shitid;}}

 二、解析資料和建立資料;

package com.bo.service;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.util.List;import com.bo.entity.ShiTiInFo;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;public class DataChuLi {public static void main(String[] args) {Gson gson = new Gson();try {String str = ReadTxtFile("F:/HTML5/shiti.txt");List<ShiTiInFo> ps1 = gson.fromJson(str,new TypeToken<List<ShiTiInFo>>() {}.getType());for (ShiTiInFo si : ps1) {System.out.println("shitia: " + si.getShitia());System.out.println("shitib: " + si.getShitib());}} catch (Exception e) {e.printStackTrace();}  }private static String ReadTxtFile(String FileName) throws Exception {BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(FileName));ByteArrayOutputStream memStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = bufferedInputStream.read(buffer)) != -1) {memStream.write(buffer, 0, len);}byte[] data = memStream.toByteArray();bufferedInputStream.close();memStream.close();bufferedInputStream.close();return new String(data);}}

 三、測試。

Gson讀寫JSON 資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.