2015-08-13 156 views
0

我想解析使用bean類的Json數據如下,但我得到的錯誤「com.google.gson。 JsonSyntaxException:java.lang.IllegalStateException:預期BEGIN_ARRAY但BEGIN_OBJECT」com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望的BEGIN_ARRAY,但是BEGIN_OBJECT在android

JSON數據

{ 
"response": true, 
"message": "Success", 
"Data": [ 
    { 
     "laboratory_id": 1000, 
     "laboratory_image": "http://abcd.com/images/laboratory.jpg", 
     "laboratory_name": "Laboratory & Research Institute", 
     "laboratory_address": "Cross Road, ", 
     "laboratory_rating": 2.5, 
     "laboratory_profile": { 
      "head_name": "Ramesh Jain", 
      "degree_name": "M.D", 
      "speciality": "Lab Technology", 
      "license_number": "34343434", 
      "laboratory": "Laboratory" 
     } 
    }, 
    { 
     "laboratory_id": 1001, 
     "laboratory_image": "http://abcd.com/images/laboratory.jpg", 
     "laboratory_name": "XYZ LAB", 
     "laboratory_address": "ASHKA ROAD", 
     "laboratory_rating": 0, 
     "laboratory_profile": { 
      "head_name": "MR X", 
      "degree_name": "MBBS", 
      "speciality": null, 
      "license_number": "123456", 
      "laboratory": "Diagnostic" 
     } 
    } 

] 

}

LaboratoryListModel

公共類LaboratoryListModel {

private String response; 
private String message; 
private ArrayList<LaboratoryList> Data = new ArrayList<LaboratoryList>(); 


public String getResponse() { 
    return response; 
} 

public void setResponse(String response) { 
    this.response = response; 
} 

public String getMessage() { 
    return message; 
} 

public void setMessage(String message) { 
    this.message = message; 
} 

public ArrayList<LaboratoryList> getData() { 
    return Data; 
} 

public void setData(ArrayList<LaboratoryList> data) { 
    Data = data; 
} 

}

LaboratoryList
公共類LaboratoryList {

private String laboratory_id; 
private String laboratory_image; 
private String laboratory_name; 
private String laboratory_address; 
private String laboratory_rating; 

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>(); 

public ArrayList<LaboratoryProfile> getLaboratory_profile() { 
    return laboratory_profile; 
} 

public void setLaboratory_profile(
     ArrayList<LaboratoryProfile> laboratory_profile) { 
    this.laboratory_profile = laboratory_profile; 
} 

public String getLaboratory_id() { 
    return laboratory_id; 
} 

public void setLaboratory_id(String laboratory_id) { 
    this.laboratory_id = laboratory_id; 
} 

public String getLaboratory_image() { 
    return laboratory_image; 
} 

public void setLaboratory_image(String laboratory_image) { 
    this.laboratory_image = laboratory_image; 
} 

public String getLaboratory_name() { 
    return laboratory_name; 
} 

public void setLaboratory_name(String laboratory_name) { 
    this.laboratory_name = laboratory_name; 
} 

public String getLaboratory_address() { 
    return laboratory_address; 
} 

public void setLaboratory_address(String laboratory_address) { 
    this.laboratory_address = laboratory_address; 
} 

public String getLaboratory_rating() { 
    return laboratory_rating; 
} 

public void setLaboratory_rating(String laboratory_rating) { 
    this.laboratory_rating = laboratory_rating; 
} 

}

LaboratoryProfile

public class LaboratoryProfile { 

private String head_name; 
private String degree_name; 
private String speciality; 
private String license_number; 
private String laboratory; 

public String getHead_name() { 
    return head_name; 
} 

public void setHead_name(String head_name) { 
    this.head_name = head_name; 
} 

public String getDegree_name() { 
    return degree_name; 
} 

public void setDegree_name(String degree_name) { 
    this.degree_name = degree_name; 
} 

public String getSpeciality() { 
    return speciality; 
} 

public void setSpeciality(String speciality) { 
    this.speciality = speciality; 
} 

public String getLicense_number() { 
    return license_number; 
} 

public void setLicense_number(String license_number) { 
    this.license_number = license_number; 
} 

public String getLaboratory() { 
    return laboratory; 
} 

public void setLaboratory(String laboratory) { 
    this.laboratory = laboratory; 
} 

}

Webservice的

public class GetLaboratoryListTask extends 
     AsyncTask<String, Integer, Object> { 

    private final Context mContext; 
    private final ProgressDialog mProgressDialog; 
    private ListView mlistView; 

    LaboratoryListModel labListModel; 

    public GetLaboratoryListTask(final Context mContext, ListView listView) { 
     this.mContext = mContext; 
     mlistView = listView; 
     mProgressDialog = new ProgressDialog(mContext); 
     mProgressDialog.setMessage("Please wait.."); 
     mProgressDialog.setCanceledOnTouchOutside(false); 
     mProgressDialog.setCancelable(false); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     if (mProgressDialog != null && !mProgressDialog.isShowing()) { 
      mProgressDialog.show(); 
     } 
    } 

    @Override 
    protected Object doInBackground(String... params) { 

     LaboratoryListModel laboratoryListModel = (LaboratoryListModel) getLaboratoryList(params[0]); 
     if (laboratoryListModel != null) 
      return laboratoryListModel.getData(); 
     else 
      return null; 

    } 

    @Override 
    protected void onPostExecute(Object result) { 
     super.onPostExecute(result); 
     if (mProgressDialog != null && mProgressDialog.isShowing()) { 
      mProgressDialog.dismiss(); 
     } 

     if (result != null) { 
      if (labListModel.getResponse().equalsIgnoreCase("true")) { 
       if (result != null && result instanceof List) { 
        mlistView.setAdapter(new SearchLabAdapter(
          LabListActivity.this, 
          (List<LaboratoryList>) result)); 
       } 
      } else { 

      } 
     } 

    } 

    private Object getLaboratoryList(String category_id) { 
     String webUrl = Constant.URL_SEARCH_LABORATORY; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 
        10000); 
      HttpResponse response; 
      JSONObject object = new JSONObject(); 
      JSONObject objectContacts = new JSONObject(); 
      try { 
       HttpPost post = new HttpPost(Constant.URL_SEARCH_LABORATORY); 

       objectContacts.put(Constant.CITY, ""); 
       objectContacts.put(Constant.LOCATION_ZIPCODE, ""); 
       objectContacts.put(Constant.LABORATORY_NAME, ""); 

       StringEntity se = new StringEntity(
         objectContacts.toString()); 

       se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, 
         "application/json")); 
       post.setEntity(se); 
       response = client.execute(post); 

       if (response != null) { 
        // 
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(response.getEntity() 
            .getContent(), "UTF-8")); 
        String json1 = reader.readLine(); 
        JSONTokener tokener = new JSONTokener(json1); 
        jObject = new JSONObject(tokener); 

       } 

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

      labListModel = (LaboratoryListModel) new Gson().fromJson(
        jObject.toString(), LaboratoryListModel.class); 

      return labListModel; 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

因爲我是新手,我不知道是什麼問題,我做了谷歌,但我並沒有得到解決,任何人都可以幫我嗎?

+0

嗨維韋克,做你檢查你的反應?它是有效的Json,它似乎是有效的,即使我建議在jsonlint.org檢查一次。 –

+0

是的,我在jsonlint.org檢查過。它是有效的JSON。 –

回答

1

關於你的評論。你需要改變你的模型。您的LaboratoryList應該只包含一個配置文件。

將其更改爲:

private LaboratoryProfile laboratory_profile; 

public LaboratoryProfile getLaboratory_profile() { 
    return laboratory_profile; 
} 
+0

thanx很多先生,它爲我工作。 –

+0

歡迎您。如果你喜歡,你也可以投票;) –

0

在你的LaboratoryList.java中有一個配置文件列表,但是你的JSON並不代表這個。它是一個JSON對象。將其更改爲數組或使配置文件列表只有一個配置文件。

你JSON的輪廓應該是這樣的:

"laboratory_profile":[ 
     { 
      "head_name":"MR X", 
      "degree_name":"MBBS", 
      "speciality":null, 
      "license_number":"123456", 
      "laboratory":"Diagnostic" 
     } 
    ] 
+0

你能幫我嗎我該怎麼辦?正如我已經提到,我是一個新手,所以我沒有太多的想法。 –

+0

我編輯了我的答案。 –

+0

還有其他方法嗎?不改變JSON我們可以做到嗎? –

1

改變你的LaboratoryList班線

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>(); 

這個

private LaboratoryProfile laboratory_profile; 

,並刪除以前的getter和setter與新的。你完成了

相關問題