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

原文地址:https://www.cnblogs.com/Rising/p/3305623.html