zoukankan      html  css  js  c++  java
  • Gson进行json字符串和对象之间的转化

    Gson可以实现对象与json字符串之间的转化,以下是在Android中的示例代码。

    Gson主页:https://code.google.com/p/google-gson/

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

    更多例子可以参见http://blog.csdn.net/lk_blog/article/details/7685169

  • 相关阅读:
    Ubuntu 12.10使用apt安装Oracle/Sun JDK
    织梦(dedecms)系统常用全局变量调用标签及路径
    Lighttpd虚拟主机和多域名的配置
    Ubuntu解压命令大全
    OFBiz终于起航了
    eclipse 安装gradle 插件的三种方式
    验证码
    session的使用
    实验二
    作业2(魔术)
  • 原文地址:https://www.cnblogs.com/Rising/p/3305623.html
Copyright © 2011-2022 走看看