[英]Parsing JSON and writing it to prefs as a list of custom object
我有一個這樣的自定義模型類 -
public class MyModel implements Parcelable {
String title;
String message;
/**
* Creator method for the Parcel to use.
*/
public static final Parcelable.Creator<MyModel> CREATOR = new Parcelable.Creator<MyModel>() {
public MyModel createFromParcel(Parcel source) {
return new MyModel(source);
}
public MyModel[] newArray(int size) {
return new MyModel[size];
}
};
public void setTitle(final String titleValue) {
title = titleValue;
}
public void setMessage(final String messageValue) {
message = messageValue;
}
public String getTitle() {
return title;
}
public String getMessage() {
return message;
}
public MyModel() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.title);
dest.writeString(this.message);
}
private MyModel(Parcel in) {
this.title = in.readString();
this.message = in.readString();
}
}
我在資產文件夾中的 JSON 是 -
[
{
"title": "1",
"message": "Hi"
},
{
"title": "2",
"message": "Bye"
},
{
"title": "3",
"message": "Ciao"
}
]
我需要讀取和解析這個 JSON 並將其作為 MyModel 對象列表寫入共享首選項。 要寫入首選項,我這樣做 -
public void setSteps(final ArrayList<MyModel> steps) {
Gson gson = new Gson();
getPrefs(mContext).edit().putString("steps", gson.toJson(steps)).apply();
}
如何解析此 JSON 並將其作為 MyModel 對象列表寫入首選項?
JSON 當前存儲在我的資產文件夾中。 稍后我可以從首選項中讀取並獲取列表
這很簡單:
Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);
public ArrayList<MyModel> getSteps(){
String localData = getPrefs(mContext).getString("steps");
return new Gson().fromJson(localData , new TypeToken<ArrayList<MyModel>>(){}.getType());
}
編寫此代碼從您的資產文件夾中讀取 JSON。
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("yourfilename.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
編寫此代碼以從您的首選項文件中讀取數組數據。
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
...
String jsonArray = getPrefs(mContext).getString("steps","[]").apply();
Type stepType = new TypeToken<ArrayList<YourModelClass>>(){}.getType();
ArrayList<YourModelClass> yourClassList = new Gson().fromJson(jsonArray, stepType);
首先從資產文件夾加載json數據到字符串:
public String loadJSONFromAsset() {
// check here data available in pref or not
// if available then return string object of pref here else fetch //from asset and set into pref
String json = null;
try {
InputStream is = getActivity().getAssets().open("yourfilename.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
此方法將返回字符串 json 文件,然后將您的字符串 json 傳遞給:
Type listType = new TypeToken<ArrayList<ModelClass>>(){}.getType();
List<ModelClass> yourClassList = new Gson().fromJson(yourJsonString, listType);
假設您的資產中的數據文件夾中有 data.json 文件。
只需嘗試以下代碼來解析您的 json。
private void getJsonData()
{
String json = null;
try {
InputStream is = getAssets().open("data/data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
}
Type listType = new TypeToken<List<MyModel>>(){}.getType();
ArrayList<MyModel> steps = new Gson().fromJson(json, listType);
setSteps(steps);
}
public void setSteps(final ArrayList<MyModel> steps) {
Gson gson = new Gson();
Log.e("~~~~~", gson.toJson(steps));
}
這是我的 logcat 結果:
E/~~~~~: [{"message":"Hi","title":"1"},{"message":"Bye","title":"2"},{"message":"Ciao","title":"3"}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.