Like the title , my json str sometimes like this:
像标题一样,我的json str有时会像这样:
{
"data": {
"changebaby": "no change",
"changemama": {
"mamacontext": "mama is a good mama",
"mamaico": "",
"mamatitle": "mama"
}
}
}
sometimes it like this:
有时像这样:
{
"data": {
"changebaby": "no change",
"changemama": "no change"
}
}
as you see,the value of the "changebaby" key and the "changemama" key sometimes is a String, sometimes is a object, how should i parse it by gson? Could anybody help me?
如你所见,“changebaby”键和“changemama”键的值有时是一个String,有时候是一个对象,我应该如何用gson解析它?有人能帮助我吗?
Don't use the android api to parse the json string, need to use the gson lib of google to parse the json string, could anybody help me?
不要使用android api来解析json字符串,需要使用google的gson lib来解析json字符串,有人可以帮帮我吗?
5 个解决方案
#1
1
if(jsonObject.optJSONObject("changemama") != null)
{
JSONObject changemama=jsonObject.optJSONObject("changemama");
//Its JSON object, do appropriate operation
}
else if(jsonObject.optString("changemama") != null)
{
//Its string, do appropriate operation
}
if you have more number of possibilities like boolean or int or long, refer this
如果你有更多的可能性,比如boolean或int或long,请参考这个
optJSONObject
optJSONObject
Returns the value mapped by name if it exists and is a JSONObject, or null otherwise.
如果存在并且是JSONObject,则返回按名称映射的值,否则返回null。
Or go with the way lawrance has given : Determine whether JSON is a JSONObject or JSONArray
或者选择lawrance给出的方式:确定JSON是JSONObject还是JSONArray
#2
1
Try with this :
试试这个:
JSONObject changemama=jsonObject.optJSONObject("changemama");
if(changemama== null){
String str=jsonObject.optString("changemama");
}
#3
0
Try this code.
试试这个代码。
JSONObject data;
try {
data = jsonObj.getJSONObject("changemama");
// do stuff
} catch (JSONException e) {
data = jsonObj.getString("changemama");
// do stuff
}
#4
0
try this :
尝试这个 :
if(obj.has("changemama")){
if(obj.optString("changemama").length() > 0){}
else if(obj.optJSONObject("changemama").length() > 0){}}
#5
0
To simplyfy android development, we can ask for the backend developers to change the Mobile API.The new API could returen the json string that cann't change the value.The value of all keys cann't sometimes be a string, sometimes a object.
为了简单地进行Android开发,我们可以要求后端开发人员更改Mobile API。新的API可以重新生成无法更改值的json字符串。所有键的值有时不能是字符串,有时候是对象。
#1
1
if(jsonObject.optJSONObject("changemama") != null)
{
JSONObject changemama=jsonObject.optJSONObject("changemama");
//Its JSON object, do appropriate operation
}
else if(jsonObject.optString("changemama") != null)
{
//Its string, do appropriate operation
}
if you have more number of possibilities like boolean or int or long, refer this
如果你有更多的可能性,比如boolean或int或long,请参考这个
optJSONObject
optJSONObject
Returns the value mapped by name if it exists and is a JSONObject, or null otherwise.
如果存在并且是JSONObject,则返回按名称映射的值,否则返回null。
Or go with the way lawrance has given : Determine whether JSON is a JSONObject or JSONArray
或者选择lawrance给出的方式:确定JSON是JSONObject还是JSONArray
#2
1
Try with this :
试试这个:
JSONObject changemama=jsonObject.optJSONObject("changemama");
if(changemama== null){
String str=jsonObject.optString("changemama");
}
#3
0
Try this code.
试试这个代码。
JSONObject data;
try {
data = jsonObj.getJSONObject("changemama");
// do stuff
} catch (JSONException e) {
data = jsonObj.getString("changemama");
// do stuff
}
#4
0
try this :
尝试这个 :
if(obj.has("changemama")){
if(obj.optString("changemama").length() > 0){}
else if(obj.optJSONObject("changemama").length() > 0){}}
#5
0
To simplyfy android development, we can ask for the backend developers to change the Mobile API.The new API could returen the json string that cann't change the value.The value of all keys cann't sometimes be a string, sometimes a object.
为了简单地进行Android开发,我们可以要求后端开发人员更改Mobile API。新的API可以重新生成无法更改值的json字符串。所有键的值有时不能是字符串,有时候是对象。