android json相关
android 对象到json json到对象
?
接口类
package com.test.json;import org.json.JSONException;import org.json.JSONObject;import org.json.JSONTokener;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class JsonTestActivity extends Activity implements OnClickListener { private Button button;private AppInfo appInfo; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); appInfo = new AppInfo(1, 1, 1, null, "1234"); }@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:objToJson();break;case R.id.button2:jsonToObj();break;default:break;}}private void objToJson(){JSONObject jsonObject = new JSONObject();try {appInfo.enCode(jsonObject);String data = jsonObject.toString();getDataContainer().edit().putString("Appinfo",data).commit();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void jsonToObj(){String jsonStr = getDataContainer().getString("Appinfo",null);if(null == jsonStr) return ;try {JSONObject object = (JSONObject) new JSONTokener(jsonStr).nextValue();appInfo = new AppInfo();appInfo.deCode(object);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private SharedPreferences getDataContainer(){return getSharedPreferences("JsonTestActivity", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);}}?