加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:05:15 所属栏目:百科 来源:网络整理
导读:最近项目中有些人使用xml存储了一些区域级联信息 (类型于山东 -(济南市,青岛市(崂山,四方)))这样的信息.使用 的是吧xml数据存储到sharepreference 感觉不是很好,费力不讨好 于是想到了json 确实,有些东西不必用到数据库 使用json和xml就可以了.个人偏好json

最近项目中有些人使用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":"u7f8eu53a8u667au5229u94f6u9cd5500g","price":"158.000","mktprice":"189.600"}</string>

</resources>
韩老师说,速度上数据库小于文件,我想这指的是小数据把?大数据还是要用数据库的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读