• Gson读写JSON 数据


    一、创建实体;

    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);
    	}
    }
    

     三、测试。

  • 相关阅读:
    AJAX 后台返回多种数据
    oracle 学习摘录
    JAVA中使用FTPClient上传下载
    struts 2 学习
    js 回车事件
    @Resource和@Autowired作用和区别
    spring 常用注解
    正则表达式
    javascript 学习教程
    cxf 发布 一个简单的 webservice
  • 原文地址:https://www.cnblogs.com/forever2698/p/4073796.html
Copyright © 2020-2023  润新知