[英]Parse and mongoDB json Object
I'm using the Parse API with mongoDB in my android application. 我在Android应用程序中将monseDB与Parse API结合使用。 In my database I have stored some values in a JSON Object like so :
在我的数据库中,我已经将一些值存储在JSON对象中,如下所示:
"address": {
"state": "blabla",
"zipcode": 00000,
"lane": "blabla",
"city": "bla"
}
Saving works like a charm : 保存就像魅力一样:
try{
JSONObject address = new JSONObject();
address.put("lane",mLane);
address.put("zipcode",mZipCode);
address.put("city",mCity);
address.put("state",mState);
user.put("address", address);
user.saveInBackground();
Toast.makeText(getContext(),"SAvedSuccessfully",Toast.LENGTH_SHORT).show();
} catch (JSONException j){
Toast.makeText(getContext(),"Error " +j,Toast.LENGTH_SHORT).show();
}
But I cannot figure out how to retrieve this data in my code. 但是我无法弄清楚如何在我的代码中检索这些数据。 It always returns a null value.
它总是返回一个空值。 It works perfectly with other non JSON objects values.
它与其他非JSON对象值完美配合。
JSONObject obj = new JSONObject();
obj = ParseUser.getCurrentUser.getJSONObject("address");
Here obj is null... 这里obj为空...
Any help would be appreciated ! 任何帮助,将不胜感激 !
You can even: first change Parse
to SharedPreferences
您甚至可以:首先将
Parse
更改为SharedPreferences
create Share 建立分享
public void putUser (String key, String value ) {
SharedPreferences pref = getSharedPreferences("YourPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(key, value);
editor.commit();
}
next create JSONObject 接下来创建JSONObject
JSONObject address = new JSONObject();
address.put("lane", "l");
address.put("zipcode", "z");
address.put("city", "c");
address.put("state", "s");
putUser("address", address.toString());
end get JSONObject 结束获取JSONObject
public String getUser (String key) {
SharedPreferences pref = getSharedPreferences("YourPref", MODE_PRIVATE);
return pref.getString(key, "");
}
JSONObject addressObject = new JSONObject(new String(getUser("address")));
if(addressObject != null) {
addressObject.getString("lane");
addressObject.getString("zipcode");
addressObject.getString("city");
addressObject.getString("state");
}
look at this example. 看这个例子。
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground()`;
I would use Gson convert to a string then store in parse. 我会使用Gson转换为字符串然后存储在解析中。
Gson gson = new Gson();
String json = gson.toJson(address);
user.put("address",json);
now get the data. 现在获取数据。
Type type = new TypeToken<JSONObject>(){}.getType();
String json = ParseUser.getCurrentUser.getJSONObject("address");
JSONObject data = gson.fromJson(type,json);
Okey I figured it out thanks to @Caspain ! Okey我想通了@Caspain了! To save the data to the DB use :
要将数据保存到数据库,请使用:
ParseUser user = ParseUser.getCurrentUser();
JSONObject address = new JSONObject();
Gson gson = new Gson();
try{
address.put("lane",mLane);
address.put("zipcode",mZipCode);
address.put("city",mCity);
address.put("state",mState);
} catch (JSONException j){
Log.i("TEST",j.toString());
}
String json = gson.toJson(address);
user.put("address", json);
user.saveInBackground();
And to retrieve the data : 并检索数据:
String json = ParseUser.getCurrentUser().getJSONObject("address").toString();
try{
JSONObject data = new JSONObject(json);
String lane = data.getString("lane"); // do whatever with your JSON Object
}catch (JSONException j){
Log.i("Erreur",j.toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.