I am trying to parse a json in my android app, I receive json properly but when I try to parse it it does not seem to parse fine!
我试图在我的Android应用程序中解析一个json,我正确地收到json但是当我尝试解析它时它似乎没有解析好!
this is the json i am receiving :
这是我收到的json:
"GetAdResult": {
"Action": null,
"ActionValue": null,
"BgColor": "blue",
"CampId": 19,
"CampType": "cpm",
"ClickAddress": null,
"EndAnimation": null,
"ExplainText": "",
"ImageType": null,
"ImageUrlBanner": null,
"ImageUrlLargeBanner": null,
"ImageUrlMediumRectangle": null,
"ImageUrlFullBanner": null,
"ImageUrlLeaderBoard": null,
"LogoUrl": "",
"MainText": "",
"PackageName": "",
"ReplaceText": "",
"StartAnimation": null,
"VideoUrl": null,
"type":"ad"
"atag":null,
"message":"success",
"pixel":null,
"script":null,
"token":null
}
and this is how i am trying to parse it:
这就是我试图解析它的方式:
public class JsonObjectSend
{
private static final String TAG_action = "Action";
private static final String TAG_acValue = "ActionValue";
private static final String TAG_bg = "BgColor";
private static final String TAG_id = "CampId";
private static final String TAG_ctype = "CampType";
private static final String TAG_adr = "ClickAddress";
private static final String TAG_endAnim = "EndAnimation";
private static final String TAG_expText = "ExplainText";
private static final String TAG_imType = "ImageType";
private static final String TAG_imgUBanner = "ImageUrlBanner";
private static final String TAG_imgULBanner = "ImageUrlLargBanner";
private static final String TAG_imgUMRect = "ImageUrlMediumRectange";
private static final String TAG_imgUFBanner = "ImageUrlFullBanner";
private static final String TAG_imgULeadBrd = "ImageUrlLeaderBoard";
private static final String TAG_logoU = "LogoUrl";
private static final String TAG_mainText = "MainText";
private static final String TAG_packName = "PackageName";
private static final String TAG_repText = "ReplaceText";
private static final String TAG_startAnim = "StartAnimation";
private static final String TAG_videoU = "VideoUrl";
private static final String TAG_type = "type";
private static final String TAG_atag = "atag";
private static final String TAG_msg = "message";
private static final String TAG_pix = "pixel";
private static final String TAG_scrpt = "script";
private static final String TAG_tkn = "token";
public void sendobj()
{
JSONObject root = new JSONObject();
JSONObject reqBody = new JSONObject();
try {
reqBody.put("DeviceId", "sdasda");
reqBody.put("AppId", "ECC7BCE40126408386BCFCF8AB9187E7");
reqBody.put("AdType", "text");
reqBody.put("Location", "35.7209331,51.472983");
reqBody.put("UserOperator", "Irancell");
reqBody.put("Ip", "127.0.0.1");
reqBody.put("NetworkType", "3G");
reqBody.put("DeviceType", "Android");
reqBody.put("DeviceBrand", "Samsung");
reqBody.put("Width", "0");
reqBody.put("Height", "0");
reqBody.put("TestMode", "true");
root.put("request", reqBody);
}
catch (JSONException e)
{
e.printStackTrace();
}
String tag_string_req = "Send_Object";
JsonObjectRequest Req = new JsonObjectRequest(Request.Method.POST, AppConfig.API_URL, root, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try
{
JSONObject jObj = response;
JSONObject GetAdResult = jObj.getJSONObject("GetAdResult");
Log.d("GetAdResult", GetAdResult.toString());
String action = GetAdResult.getString(TAG_action);
String actionvalue = GetAdResult.getString(TAG_acValue);
String bg = GetAdResult.getString(TAG_bg);
String campid = GetAdResult.getString(TAG_id);
String camptype = GetAdResult.getString(TAG_ctype);
String clickadr = GetAdResult.getString(TAG_adr);
String endanim = GetAdResult.getString(TAG_endAnim);
String exptext = GetAdResult.getString(TAG_expText);
String imgtype = GetAdResult.getString(TAG_imType);
String imgurlbanner = GetAdResult.getString(TAG_imgUBanner);
String imgurllargebanner = GetAdResult.getString(TAG_imgULBanner);
String imgurlmedrectangle = GetAdResult.getString(TAG_imgUMRect);
String imgurlfullbanner = GetAdResult.getString(TAG_imgUFBanner);
String imgurlleaderboard = GetAdResult.getString(TAG_imgULeadBrd);
String logourl = GetAdResult.getString(TAG_logoU);
String maintext = GetAdResult.getString(TAG_mainText);
String packagename = GetAdResult.getString(TAG_packName);
String replacetext = GetAdResult.getString(TAG_repText);
String startenim = GetAdResult.getString(TAG_startAnim);
String videourl = GetAdResult.getString(TAG_videoU);
String type = GetAdResult.getString(TAG_type);
String atag = GetAdResult.getString(TAG_atag);
String msg = GetAdResult.getString(TAG_msg);
String pixel = GetAdResult.getString(TAG_pix);
String script = GetAdResult.getString(TAG_scrpt);
String token = GetAdResult.getString(TAG_tkn);
Log.d("hg3uewygrhefkj","ufdjklc");
Log.d("action", action);
Log.d("actionvalue",actionvalue);
Log.d("Back",bg);
/*
String errorMsg = jObj.getString("error_msg");
Log.d("Debug", errorMsg);
}*/
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", "Error");
Log.i("onErrorResponse", error.toString());
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
// Now you can use any deserializer to make sense of data
JSONObject jsonbody = new JSONObject(res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
} catch (JSONException e2) {
// returned data is not JSONObject?
e2.printStackTrace();
}
}
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(Req, tag_string_req);
}
}//class end
but when I try to Log.d the strings there is nothing in my logcat. how else am i suppose to do such thing?
但是当我尝试Log.d时,我的logcat中没有任何内容。我怎么想做这样的事呢?
4 个解决方案
#1
1
I will suggest you to crate a POJO to represent this JSON object, and use Gson library to parse.
我建议你创建一个POJO来表示这个JSON对象,并使用Gson库进行解析。
#2
1
I posted an answer on here that might be able to help you out. So in your case, if you want to get "BgColor", you can do this:
我在这里发布了一个可以帮助你的答案。所以在你的情况下,如果你想获得“BgColor”,你可以这样做:
public String parseJSONForTranslation(String jsonString) {
try {
JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue();
return object.getJSONObject("GetAdResult").getString("BgColor");
}
catch (JSONException e) {
return null;
}
}
}
#3
0
In your json response, there is no GetAdResult object which you are actually referring to. With the above code, "GetAdResult" should come in response and it should be root element.
在您的json响应中,没有您实际引用的GetAdResult对象。使用上面的代码,“GetAdResult”应该响应,它应该是根元素。
Just remove "GetAdResult" because it is not there as root element. Try with -> JSONObject GetAdResult = jObj.getJSONObject();
只需删除“GetAdResult”,因为它不是根元素。尝试使用 - > JSONObject GetAdResult = jObj.getJSONObject();
It will give you json object and then you can get all the string elements.
它将为您提供json对象,然后您可以获取所有字符串元素。
#4
0
try below code
尝试以下代码
public String parseJSON(String jsonString) {
try {
尝试{
JSONObject object = new JSONObject(jsonString);
JSONObject adResultObj=object.optJSONObject("GetAdResult");
String bgColor=adResiltObj.optString("BgColor");
String campId=adResiltObj.optString("CampId");
} catch (Exception e) {
return null;
}
In your code just replace your first line with my first line i.e replace below
在你的代码中,只需用我的第一行替换你的第一行,即在下面替换
JSONObject jObj = response;
JSONObject jObj =响应;
with
同
JSONObject object = new JSONObject(response);
JSONObject object = new JSONObject(response);
#1
1
I will suggest you to crate a POJO to represent this JSON object, and use Gson library to parse.
我建议你创建一个POJO来表示这个JSON对象,并使用Gson库进行解析。
#2
1
I posted an answer on here that might be able to help you out. So in your case, if you want to get "BgColor", you can do this:
我在这里发布了一个可以帮助你的答案。所以在你的情况下,如果你想获得“BgColor”,你可以这样做:
public String parseJSONForTranslation(String jsonString) {
try {
JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue();
return object.getJSONObject("GetAdResult").getString("BgColor");
}
catch (JSONException e) {
return null;
}
}
}
#3
0
In your json response, there is no GetAdResult object which you are actually referring to. With the above code, "GetAdResult" should come in response and it should be root element.
在您的json响应中,没有您实际引用的GetAdResult对象。使用上面的代码,“GetAdResult”应该响应,它应该是根元素。
Just remove "GetAdResult" because it is not there as root element. Try with -> JSONObject GetAdResult = jObj.getJSONObject();
只需删除“GetAdResult”,因为它不是根元素。尝试使用 - > JSONObject GetAdResult = jObj.getJSONObject();
It will give you json object and then you can get all the string elements.
它将为您提供json对象,然后您可以获取所有字符串元素。
#4
0
try below code
尝试以下代码
public String parseJSON(String jsonString) {
try {
尝试{
JSONObject object = new JSONObject(jsonString);
JSONObject adResultObj=object.optJSONObject("GetAdResult");
String bgColor=adResiltObj.optString("BgColor");
String campId=adResiltObj.optString("CampId");
} catch (Exception e) {
return null;
}
In your code just replace your first line with my first line i.e replace below
在你的代码中,只需用我的第一行替换你的第一行,即在下面替换
JSONObject jObj = response;
JSONObject jObj =响应;
with
同
JSONObject object = new JSONObject(response);
JSONObject object = new JSONObject(response);