您的位置:首页 > Web前端

使用xml sharepreference等存放json信息并读取

2012-11-26 10:47 225 查看
最近项目中有些人使用xml存储了一些区域级联信息 (类型于山东 -(济南市,青岛市(崂山,四方)))这样的信息.使用 的是吧xml数据存储到sharepreference 感觉不是很好,费力不讨好

于是想到了json

确实,有些东西不必用到数据库 使用json和xml就可以了.个人偏好json

package com.example.testjsonandxml;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String json = getString(R.string.json);

		DataStoreUtil.put(this, "json", json);

		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(json);
			TextView textView = new TextView(this);
			textView.setText(jsonObject.getString("name"));
			textView.append(jsonObject.getString("price"));

			textView.append("来自SharePreference:"+new JSONObject(DataStoreUtil.getString(
					MainActivity.this, "json")).getString("mktprice"));
			setContentView(textView);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


string.xml中储存的信息

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">TestJsonAndXml</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="json">{"goods_id":"1002109","name":"\u7f8e\u53a8\u667a\u5229\u94f6\u9cd5500g","price":"158.000","mktprice":"189.600"}</string>

</resources>


韩老师说,速度上数据库小于文件,我想这指的是小数据把?大数据还是要用数据库的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: