日期:2014-05-16 浏览次数:20328 次
android 对象到json json到对象
?
接口类
package com.test.json; import org.json.JSONException; import org.json.JSONObject; public interface JsonSerializable { abstract void deCode(JSONObject object) throws JSONException; abstract void enCode(JSONObject object) throws JSONException; }?
实体类
package com.test.json; import java.io.Serializable; import org.json.JSONException; import org.json.JSONObject; public class ItemInfo implements Serializable , JsonSerializable{ /** * */ private static final long serialVersionUID = 1L; public int screen; public int cellX; public int cellY; public int mWidth; public int mHeight; public Object ObjectTag; public ItemInfo(){} public ItemInfo(int screen,int cellX, int cellY) { super(); this.screen = screen; this.cellX = cellX; this.cellY = cellY; } public ItemInfo(int screen,int cellX, int cellY, int mWidth, int mHeight, Object objectTag) { super(); this.screen = screen; this.cellX = cellX; this.cellY = cellY; this.mWidth = mWidth; this.mHeight = mHeight; ObjectTag = objectTag; } @Override public void enCode(JSONObject object) throws JSONException { object.put("screen", screen); object.put("cellX", cellX); object.put("cellY", cellY); } @Override public void deCode(JSONObject object) throws JSONException { screen = object.getInt("screen"); cellX = object.getInt("cellX"); cellY = object.getInt("cellY"); } }?
?
package com.test.json; import org.json.JSONException; import org.json.JSONObject; import android.graphics.Bitmap; public class AppInfo extends ItemInfo { /** * */ private static final long serialVersionUID = 1L; public Bitmap iconBitmap; public String title; public boolean isInstall = false; public boolean isUpdate = false; public AppInfo(){ super(); } public AppInfo(Bitmap iconBitmap, String title) { super(); this.iconBitmap = iconBitmap; this.title = title; } public AppInfo(int screen, int cellX, int cellY, Bitmap iconBitmap, String title) { super(screen, cellX, cellY); this.iconBitmap = iconBitmap; this.title = title; } public boolean eqXY(AppInfo appInfo) { if (this.cellX == appInfo.cellX && this.cellY == appInfo.cellY) return true; return false; } @Override public void enCode(JSONObject object) throws JSONException { super.enCode(object); object.put("title", title); object.put("isInstall", isInstall); object.put("isUpdate", isUpdate); } @Override public void deCode(JSONObject object) throws JSONException { super.deCode(object); title = object.getString("title"); isInstall = object.getBoolean("isInstall"); isUpdate = object.getBoolean("isUpdate"); } }
?
activity
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"); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: objToJson(); break