您的位置:首页 > 移动开发 > Android开发

android 保存Json数据到本地

2016-05-25 10:50 405 查看
android 保存Json数据到本地

一:摘要

本文主要介绍一些Json的基本知识,包括数据的保存和解析,但更重要的是使用Json保存一些非常规的数据,提供一种保存数据的方式。例如,常规的Json不支持保存Bitmap的数据,我们可以保存Bitmap,将Bitmap作为Json的value。

二:Json简介

Json是一种键值对存在的数据形式,值一般可以有四种形式的值。


Json值的数据结构

    Tips:{}表示基本的Json数据,[]表示数组,涉及到基本数据类型使String用“”,int不需要,每条数据使用都好隔开,键值对中间使用冒号(:)隔开。

    Json值的结构可以包含四种格式的数据:

    1:简单的Json数据;

    { ,,,,}

    例如:{"userName":"liuhaiyong","userAge":26,"addr":"China"}

    2:里面有数组;

    例如:{"userName":"liuhaiyong","userAge":26,"shuzu":[{"zhengshu":"CET-6","zhengshu":"xueshi","data":"2016"},{"school":"xinhai","xueye":"gaozhong"}],"addr":"China"}

    3:直接一个数组;

    [{"zhengshu":"CET-6","zhengshu":"xueshi","data":"2016"},{"school":"xinhai","xueye":"gaozhong"}]

    4:数组里面有数组;

    [

      {"devid":"1234567800","gps":[{"time":"2014-11-12","latitude":"29.4963","longitude":"116.189"},{"time":"2014-11-12","latitude":"29.4963","longitude":"116.189" }],"devname":"赣01"},

        {"devid":"1234567800","gps":[{"time":"2014-11-12","latitude":"29.4963","longitude":"116.189"},{"time":"2014-11-12","latitude":"29.4963","longitude":"116.189" }],"devname":"赣92"},

        {"devid":"1234567800","gps":[{"time":"2014-11-12","latitude":"29.4963","longitude":"116.189"},{"time":"2014-11-12","latitude":"29.4963","longitude":"116.189" }],"devname":"赣43"},

      ]


Java构建Json数据对象

    1:类:JSONObject,JSONStringer,JSONArray

    JSONObject可以看作一个Json对象,他是Json的一个基本单元,其包含一个键值对,对外输出标准的字符串单元,最外面被大括号包含。使用put方法添加树枝,key与value之间用逗号分开,Value的类型可以是:Boolean,JSONArray,JSONObject,Number,String,JSONObject.null,Object;
package com.example.lhy.jsonparser.util;

import android.util.Log;

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

/**
* Created by lhy on 16-5-24.
*/
public class Data {
public static final String TAG = "liuhaiyong";
public static final String DATA_JSON_SIMPLE = "{\"name\":\"lhy\",\"height\":\"177cm\",\"weight\":\"55kg\"}";
public static final String DATA_JSON_ARRAY = "[{\"last\":\"PHICOMM\",\"now\":\"huiye\"},{\"school\":\"SHU\"}]";
public static final String DATA_JSON_ARRAY_ = "[\"name\",\"height\",\"weight\"]";
public static final String DATA_JSON_ARRAY_INNER = "{\"name\":\"lhy\",\"age\":26,\"resume\":[{\"last\":\"PHICOMM\",\"now\":\"huiye\"},{\"school\":\"SHU\"}]}";
public static final String DATA_JSON_ARRAY_ARRAY_INNER = "[{\"last\":\"PHICOMM\",\"now\":\"huiye\"},{\"school\":[{\"gaozhong\":\"xinhai\"},{\"daxue\":\"SHU\"}]}]";

public static JSONObject getJsonSimple() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", "lhy");
jsonObject.put("height", "177cm");
jsonObject.put("weight", "55kg");
} catch (JSONException e) {
throw new RuntimeException("getJsonSimple JSONException:" + e);
}
Log.d(TAG, "getJsonSimple jsonObject:" + jsonObject.toString());
Log.e(TAG, "getJsonSimple jsonObject:" + DATA_JSON_SIMPLE);
return jsonObject;
}

public static JSONArray getJsonArray() {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
try {
jsonObject1.put("last", "PHICOMM");
jsonObject1.put("now", "huiye");
jsonObject2.put("school", "SHU");
jsonArray.put(jsonObject1).put(jsonObject2);
} catch (JSONException e) {
throw new RuntimeException("getJsonArray JSONException:" + e);
}
Log.d(TAG, "getJsonArray JSONArray:" + jsonArray.toString());
Log.e(TAG, "getJsonArray JSONArray:" + DATA_JSON_ARRAY);
return jsonArray;
}

public static JSONObject getJsonArrayInner() {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArrayInner = new JSONArray();
try {
jsonObject.put("name", "lhy");
jsonObject.put("age", 26);

JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("last", "PHICOMM0").put("now", "huiye");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("school", "SHU");
jsonArrayInner.put(jsonObject1).put(jsonObject2);
jsonObject.put("resume", jsonArrayInner);
} catch (JSONException e) {
throw new RuntimeException("getJsonArrayInner JSONException:" + e);
}
Log.d(TAG, "getJsonArrayInner JSONObject:" + jsonObject.toString());
Log.e(TAG, "getJsonArrayInner JSONObject:" + DATA_JSON_ARRAY_INNER);
return jsonObject;
}

public static JSONArray getJsonArrayArrayInner() {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();

JSONArray jsonArrayInner = new JSONArray();
JSONObject jsonObjectInner1 = new JSONObject();
JSONObject jsonObjectInner2 = new JSONObject();

try {
jsonObject1.put("last", "PHICOMM").put("now", "huiye");
jsonArray.put(jsonObject1);

jsonObjectInner1.put("gaozhong", "xinhai");
jsonArrayInner.put(jsonObjectInner1);
jsonObjectInner2.put("daxue", "SHU");
jsonArrayInner.put(jsonObjectInner2);
jsonObject2.put("school", jsonArrayInner);
jsonArray.put(jsonObject2);
} catch (JSONException e) {
throw new RuntimeException("getJsonArrayArrayInner JSONException:" + e);
}
Log.d(TAG, "getJsonArrayArrayInner JSONArray:" + jsonArray.toString());
Log.e(TAG, "getJsonArrayArrayInner JSONArray:" + DATA_JSON_ARRAY_ARRAY_INNER);
return jsonArray;
}

}
 
JSONStringer:json文本构建类(掌握使用方法就可以,和JSONObject目的一样);

    JSONArray:它代表一组有序的数值。将其转换为数据表现形式是外围被[]包裹的字符串。

三:Json的解析

    JSONTokener,Json解析类。

    JSONException:异常类。

四:保存非常规数据

    我们可以使用一些手段,将需要的数据转化为byte,再将byte转化成为String,也可以将String反转为byte,然后再解析为原数据
static byte[] flattenBitmap(Bitmap bitmap) {
// Try go guesstimate how much space the icon will take when serialized
// to avoid unnecessary allocations/copies during the write.
int size = bitmap.getWidth() * bitmap.getHeight() * 4;
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return out.toByteArray();
} catch (IOException e) {
PhiLogPrinter.logw("Favorite", "Could not write icon");
return null;
}
}

Base64.encodeToString(iconByteArray, 0, iconByteArray.length, Base64.DEFAULT));

String iconBase64 =parser	//parser是从Json数据里面取出来的经过编码处理的数据
byte[] iconArray = Base64.decode(iconBase64, Base64.DEFAULT);
Bitmap b = BitmapFactory.decodeByteArray(iconArray, 0, iconArray.length);

未完待续
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: