[英]Java value of type java.lang.string cannot be converted to jsonobject
[英]Value Array of type java.lang.String cannot be converted to JSONObject
我已經開始 android 編程,最近遇到了幾個問題。 我正在從數據庫中獲取數據並使用 POSTMAN,我可以在 JSON 中看到如下所示的結果,但是在解析它時,它給了我錯誤:
{
"result": [
{
"Date": "18-3-2016",
"Events": "Local Holiday"
},
{
"Date": "23-3-2016",
"Events": "Monthly Fees"
},
{
"Date": "15-4-2016",
"Events": "Monthly Fees"
},
{
"Date": "23-4-2016",
"Events": "Annual Day"
},
{
"Date": "30-4-2016",
"Events": "session end"
},
{
"Date": "9-4-2016",
"Events": "Parent Teacher Meeting"
}
]
}
我正在使用代碼來顯示,但出現錯誤,我的代碼是:
private void showJSON(String response) {
String child_name = "";
String Date ="";
String address="";
//String vc = "";
try {
final String TAG = events.class.getSimpleName();
Log.d(TAG, "showJSON: \n"+response);
JSONObject jsonObject = new JSONObject(response);
//Get the instance of JSONArray that contains JSONObjects
// JSONArray jsonArray = jsonRootObject.optJSONArray(config_events.JSON_ARRAY);
JSONArray result = jsonObject.getJSONArray(config_events.JSON_ARRAY);
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < result.length(); i++) {
//JSONObject jsonObject = jsonArray.getJSONObject(i);
//JSONObject jsonObject = new JSONObject(response);
Log.d(TAG, "showJSON: Event list size: "+result.length());
JSONObject collegeData = result.getJSONObject(0);
child_name = collegeData.getString(config_events.KEY_NAME);
address = collegeData.getString(config_events.KEY_ADDRESS);
//vc = collegeData.getString(config.KEY_VC);
textViewResult.setText("EVENTS:\t" + address);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
錯誤:
Value Array of type java.lang.String cannot be converted to JSONObject
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at org.json.JSONObject.<init>(JSONObject.java:158)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at org.json.JSONObject.<init>(JSONObject.java:171)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.weavearound.schools.weavearound.events.showJSON(events.java:105)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.weavearound.schools.weavearound.events.access$100(events.java:25)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.weavearound.schools.weavearound.events$1.onResponse(events.java:82)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.weavearound.schools.weavearound.events$1.onResponse(events.java:78)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at android.os.Handler.handleCallback(Handler.java:605)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at android.os.Looper.loop(Looper.java:137)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4517)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err: at dalvik.system.NativeStart.main(Native Method)
響應代碼:
String url = config_events.DATA_URL;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(events.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest
)
您正在發出字符串請求。 改為創建一個 JsonObjectRequest。 然后你會收到這樣的 json 響應 -
showJSON(JsonObject response)
然后像這樣從中取出結果 -
JSONArray result = response.getJSONArray("result");
你能改變那些行嗎......可以肯定
JSONArray result = jsonObject.getJSONArray("result");
for (int i = 0; i < result.length(); i++) {
JSONObject collegeData = result.getJSONObject(i);
child_name = collegeData.getString("Date");
address = collegeData.getString("Events");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.