android - JSONException : java. lang.Integer 无法转换为 JSONObject`

标签 android json listview

我试图在他/她登录后在 ListView 中显示每个学生所学的科目。我使用共享首选项将用户从登录页面带入。 JSONParser 出错了吗?

logcat 错误。

   03-30 18:02:26.457: I/System.out(28204): Response : User Found
   03-30 18:02:26.715: D/Response:(28204): 123{"success":1,"student":   [{"matrix_id":"123","ic_no":"123","name":"ALI","email":"ALI@yahoo.com"}]}
   03-30 18:02:26.723: W/System.err(28204): org.json.JSONException: Value 123 of type java.lang.Integer cannot be converted to JSONObject
   03-30 18:02:26.731: W/System.err(28204):     at org.json.JSON.typeMismatch(JSON.java:111)
   03-30 18:02:26.731: W/System.err(28204):     at org.json.JSONObject.<init>(JSONObject.java:158)
   03-30 18:02:26.731: W/System.err(28204):     at org.json.JSONObject.<init>(JSONObject.java:171)
   03-30 18:02:26.731: W/System.err(28204):     at com.ultra.esc.ScheduleFragment$LoadAllSubject.doInBackground(ScheduleFragment.java:146)
   03-30 18:02:26.731: W/System.err(28204):     at com.ultra.esc.ScheduleFragment$LoadAllSubject.doInBackground(ScheduleFragment.java:1)
   03-30 18:02:26.731: W/System.err(28204):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
  03-30 18:02:26.731: W/System.err(28204):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  03-30 18:02:26.731: W/System.err(28204):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  03-30 18:02:26.731: W/System.err(28204):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  03-30 18:02:26.731: W/System.err(28204):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 03-30 18:02:26.731: W/System.err(28204):   at java.lang.Thread.run(Thread.java:856)

这是登录后的代码。

   //Bring the value from login page----------------------------------------------
   SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
   matrix_id = settings.getString("matrix","");

   TextView matrix = (TextView)rootView.findViewById(R.id.textMatrix);      
   matrix.setText(settings.getString("matrix", "XXXXX"));

将值传递给 JSON 代码。

  //protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // post matrix_id as GET parameters
        params.add(new BasicNameValuePair("matrix_id", matrix_id));

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(url_all_subjects, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("Response: ", json);          

        try {
            JSONObject jObj = new JSONObject(json); 

            if(jObj != null){
                String matrix_id = jObj.getString(TAG_MATRIX_ID);
                subject = jObj.getJSONArray(TAG_SUCCESS);

                if (subject != null) {
                // looping through All Subjects
                for (int i = 0; i < subject.length(); i++) {
                    JSONObject c = subject.getJSONObject(i);

                    // Storing each json item in variable
                    String subject = c.getString(TAG_SUBJECT);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("matrix_id", matrix_id);
                    map.put(TAG_SUBJECT, subject);

                    // adding HashList to ArrayList
                    subjectList.add(map);
                }
            }
        }
        else 
        {   Log.d("Subjects: ", "null");}

        }
         catch (JSONException e) 
         {
            e.printStackTrace();
        }

        return null;
    }

   protected void onPostExecute(String file_url) {
        super.onPostExecute(file_url);
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread           
                 ListAdapter adapter = new SimpleAdapter(
                           getActivity(), subjectList,
                        R.layout.all_subject, new String[] {TAG_MATRIX_ID,TAG_SUBJECT},
                        new int[] { R.id.matrix_id, R.id.name });
                // updating listview
                myListView.setAdapter(adapter);
    }

最佳答案

问题似乎是您正在获取一个数组作为对象。试试这个:

SomeStudentModel model;
ArrayList<SomeStudentModel> typeList = new ArrayList<>();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

JSONObject jsonobject;

 try {

      JSONArray result = response.getJSONArray("student");
          for (int i = 0; i < result.length(); i++) {
             jsonobject = result.getJSONObject(i);
             brand = gson.fromJson(jsonobject.toString(),
             SomeStudentModel.class);
             typeList.add(brand);
          }
      } catch (JSONException e) {
            e.printStackTrace();
      }

祝你好运!

关于android - JSONException : java. lang.Integer 无法转换为 JSONObject`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741967/

相关文章:

android - Spring 框架工作 : 500 Internal Server Error

java - 更新 ListView 不起作用

android - 关于Android中SQLite的更新

android - 使用大图时如何使用JNI位图操作避免OOM?

android - 错误 : package R does not exist how to debug when using command line

android - 音频播放完成后如何更改播放图标按钮

c# - Json.Net 忽略 Unity 中的序列化私有(private)字段

ios - 无法将 JSONValue 写入 NSDictionary - JSON 框架无法正常工作

java - 通知数据集更改();不刷新ListView?

Android:Sax 解析为 ListView