• 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

  • 相关阅读:
    Runoob-JSP:JSP 表单处理
    Runoob-JSP:JSP 状态码
    Runoob-JSP:JSP 服务器响应
    extundelete
    Java实现 洛谷 P1601 A+B Problem(高精)
    Java实现 洛谷 P1601 A+B Problem(高精)
    Java实现 洛谷 P1601 A+B Problem(高精)
    Java实现 洛谷 P1508 Likecloud-吃、吃、吃
    Java实现 洛谷 P1508 Likecloud-吃、吃、吃
    Java实现 洛谷 P1508 Likecloud-吃、吃、吃
  • 原文地址:https://www.cnblogs.com/Rising/p/3305623.html
Copyright © 2020-2023  润新知