gosn 解析

public class GsonActivity extends Activity {
    Button saveButton;
    Button loadButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gsonlayout);
        initControls();
    }
    
    protected void initControls(){
        saveButton = (Button) findViewById(R.id.btSave);
        loadButton = (Button) findViewById(R.id.btGsonLoad);
        
        saveButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                List<StudentInfo> studentInfos = new LinkedList<StudentInfo>() ;
                StudentInfo s1 = new StudentInfo();
                s1.setId(1);
                s1.setName("张三");
                s1.setAddress("武汉市");
                s1.setPhone("12345671");
                studentInfos.add(s1);
                
                StudentInfo s2 = new StudentInfo();
                s2.setId(2);
                s2.setName("李四");
                s2.setAddress("华工");
                s2.setPhone("12345672");
                studentInfos.add(s2);
                
                 Gson gson = new Gson(); 
                 String json = gson.toJson(studentInfos);
                try {
                    FileOutputStream fs = openFileOutput("gsonconfig.xml", MODE_PRIVATE);
                    fs.write(json.getBytes());
                    fs.close();
                    Toast.makeText(GsonActivity.this, json, Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(GsonActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
        
        loadButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                List<StudentInfo> studentInfos = new LinkedList<StudentInfo>();
                String json = "";
                
                try {
                    FileInputStream fileInputStream = openFileInput("gsonconfig.xml");
                    InputStreamReader inputStreamReader =new InputStreamReader(fileInputStream);
                    BufferedReader bufferedReader = new  BufferedReader(inputStreamReader);
                    
                    json = bufferedReader.readLine();
                    bufferedReader.close();
                    
                    Gson gson = new    Gson();
                    studentInfos = gson.fromJson(json, new TypeToken<List<StudentInfo>>() {  
                    }.getType());
                    for (StudentInfo studentInfo : studentInfos) {
                        Toast.makeText(GsonActivity.this, studentInfo.toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值