2017-09-05 69 views
0

我將把json數據轉換成json數組,但是我得到的錯誤是: - 輸入org.json。 JSONObject不能轉換爲JSONArray。如何解決這個問題。 Click here view the json data 私人無效populateDashBoardReport(束束){如何將給定的json轉換爲json數組

if (getContext() != null && bundle != null && bundle.getLong("TenantId") != 0L) { 

     isDataLoaded = true; // indicate that the data was loaded already 

     mTenantId = bundle.getLong("TenantId"); 
     boolean isTenant = bundle.getBoolean("IsTenant"); 
     long userId = (isTenant ? 0 : bundle.getLong("UserId")); 

     String url = CashPunditUtils.BaseUrl + "/GetDashBoard?TenantId=" + mTenantId + "&AppUserId=" + userId; 
     final ProgressDialog progressDialog; 

     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { 
      progressDialog = new ProgressDialog(getContext(), android.support.v7.appcompat.R.style.Theme_AppCompat_Light_Dialog_Alert); 
     } else { 
      progressDialog = new ProgressDialog(getContext()); 
     } 

     progressDialog.setMessage("Loading..."); 
     progressDialog.show(); 

     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 

       progressDialog.dismiss(); 

       mDashBoardVOsList = parseJsonResponse(response); 

       // set adapter for recycler view 
       mRecyclerViewAdapter = new MyRecyclerViewAdapter(mDashBoardVOsList); 
       mRecyclerView.setAdapter(mRecyclerViewAdapter); 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

       progressDialog.dismiss(); 

       Log.e(TAG, "Error occured on DashBoard request :" + error.getMessage()); 

       Toast.makeText(getContext(), "Error loading DashBoard report :" + error.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
     // Change the timeout to 1 minute 
     jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

     // Adding request to volley request queue 
     VolleySingleton.getInstance(getContext()).addToRequestQueue(jsonArrayRequest); 
    } 
} 

私人列表parseJsonResponse(JSONArray響應){

List<DashBoardVO> dashboardVOsList = null; 

    if (response != null) { 

     dashboardVOsList = new ArrayList<>(); 

     for (int index = 0; index < response.length(); index++) { 

      DashBoardVO dashBoardVO = new DashBoardVO(); 

      try { 
       JSONObject jsonObject = response.getJSONObject(index); 

       dashBoardVO.setReceivable(jsonObject.getDouble("Receivable")); 
       dashBoardVO.setPayable(jsonObject.getDouble("Payable")); 

       dashboardVOsList.add(dashBoardVO); 

      } catch (JSONException e) { 
       Log.e(TAG, "Error occured while parsing JSON :" + e.getMessage()); 
      } 
     } 
    } 
    return dashboardVOsList; 
} 
+0

我添加了代碼,將值設置爲 –

回答

0

更改爲向JsonObjectRequest

JsonObjectRequest jsonObjectRequest= new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 

       progressDialog.dismiss(); 

       mDashBoardVOsList = parseJsonResponse(response); 

       // set adapter for recycler view 
       mRecyclerViewAdapter = new MyRecyclerViewAdapter(mDashBoardVOsList); 
       mRecyclerView.setAdapter(mRecyclerViewAdapter); 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

       progressDialog.dismiss(); 

       Log.e(TAG, "Error occured on DashBoard request :" + error.getMessage()); 

       Toast.makeText(getContext(), "Error loading DashBoard report :" + error.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     }); 

,改變JSON解析這個

private List parseJsonResponse(JSONObject response) { 

    List<DashBoardVO> dashboardVOsList = null; 

    if (response != null) { 
     JSONArray array=response.getJSONArray("lstGraph"); 
     dashboardVOsList = new ArrayList<>(); 

     for (int index = 0; index < array.length(); index++) { 

      DashBoardVO dashBoardVO = new DashBoardVO(); 

      try { 
       JSONObject jsonObject = array.getJSONObject(index); 

       dashBoardVO.setReceivable(jsonObject.getDouble("Receivable")); 
       dashBoardVO.setPayable(jsonObject.getDouble("Payable")); 

       dashboardVOsList.add(dashBoardVO); 

      } catch (JSONException e) { 
       Log.e(TAG, "Error occured while parsing JSON :" + e.getMessage()); 
      } 
     } 
    } 
    return dashboardVOsList; 
} 

更新:

您可以使用Gson庫將JSON轉換爲Java Pojo類。

添加GSON依賴於你的build.gradle

compile 'com.google.code.gson:gson:2.8.1' 

添加以下車型

-----------------------------------com.example.DashboardVOsList.java----------------------------------- 

package com.example; 

import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class DashboardVOsList { 

@SerializedName("Receivable") 
@Expose 
private Double receivable; 
@SerializedName("Payable") 
@Expose 
private Double payable; 
@SerializedName("oPendingFollowup") 
@Expose 
private OPendingFollowup oPendingFollowup; 
@SerializedName("oPDCReceive") 
@Expose 
private OPDCReceive oPDCReceive; 
@SerializedName("oInvOverdue") 
@Expose 
private OInvOverdue oInvOverdue; 
@SerializedName("oInvAlmostOverdue") 
@Expose 
private OInvAlmostOverdue oInvAlmostOverdue; 
@SerializedName("lstGraph") 
@Expose 
private List<LstGraph> lstGraph = null; 

public Double getReceivable() { 
return receivable; 
} 

public void setReceivable(Double receivable) { 
this.receivable = receivable; 
} 

public Double getPayable() { 
return payable; 
} 

public void setPayable(Double payable) { 
this.payable = payable; 
} 

public OPendingFollowup getOPendingFollowup() { 
return oPendingFollowup; 
} 

public void setOPendingFollowup(OPendingFollowup oPendingFollowup) { 
this.oPendingFollowup = oPendingFollowup; 
} 

public OPDCReceive getOPDCReceive() { 
return oPDCReceive; 
} 

public void setOPDCReceive(OPDCReceive oPDCReceive) { 
this.oPDCReceive = oPDCReceive; 
} 

public OInvOverdue getOInvOverdue() { 
return oInvOverdue; 
} 

public void setOInvOverdue(OInvOverdue oInvOverdue) { 
this.oInvOverdue = oInvOverdue; 
} 

public OInvAlmostOverdue getOInvAlmostOverdue() { 
return oInvAlmostOverdue; 
} 

public void setOInvAlmostOverdue(OInvAlmostOverdue oInvAlmostOverdue) { 
this.oInvAlmostOverdue = oInvAlmostOverdue; 
} 

public List<LstGraph> getLstGraph() { 
return lstGraph; 
} 

public void setLstGraph(List<LstGraph> lstGraph) { 
this.lstGraph = lstGraph; 
} 

} 
-----------------------------------com.example.LstGraph.java----------------------------------- 

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class LstGraph { 

@SerializedName("Receivable") 
@Expose 
private Double receivable; 
@SerializedName("Payable") 
@Expose 
private Double payable; 
@SerializedName("CashFlow") 
@Expose 
private Double cashFlow; 
@SerializedName("CFDate") 
@Expose 
private String cFDate; 

public Double getReceivable() { 
return receivable; 
} 

public void setReceivable(Double receivable) { 
this.receivable = receivable; 
} 

public Double getPayable() { 
return payable; 
} 

public void setPayable(Double payable) { 
this.payable = payable; 
} 

public Double getCashFlow() { 
return cashFlow; 
} 

public void setCashFlow(Double cashFlow) { 
this.cashFlow = cashFlow; 
} 

public String getCFDate() { 
return cFDate; 
} 

public void setCFDate(String cFDate) { 
this.cFDate = cFDate; 
} 

} 
-----------------------------------com.example.OInvAlmostOverdue.java----------------------------------- 

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class OInvAlmostOverdue { 

@SerializedName("Customers") 
@Expose 
private Integer customers; 
@SerializedName("Invoices") 
@Expose 
private Integer invoices; 
@SerializedName("PerRec") 
@Expose 
private Double perRec; 
@SerializedName("InvValue") 
@Expose 
private Double invValue; 

public Integer getCustomers() { 
return customers; 
} 

public void setCustomers(Integer customers) { 
this.customers = customers; 
} 

public Integer getInvoices() { 
return invoices; 
} 

public void setInvoices(Integer invoices) { 
this.invoices = invoices; 
} 

public Double getPerRec() { 
return perRec; 
} 

public void setPerRec(Double perRec) { 
this.perRec = perRec; 
} 

public Double getInvValue() { 
return invValue; 
} 

public void setInvValue(Double invValue) { 
this.invValue = invValue; 
} 

} 
-----------------------------------com.example.OInvOverdue.java----------------------------------- 

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class OInvOverdue { 

@SerializedName("Customers") 
@Expose 
private Integer customers; 
@SerializedName("Invoices") 
@Expose 
private Integer invoices; 
@SerializedName("InvValue") 
@Expose 
private Double invValue; 
@SerializedName("YetToFollow") 
@Expose 
private Integer yetToFollow; 

public Integer getCustomers() { 
return customers; 
} 

public void setCustomers(Integer customers) { 
this.customers = customers; 
} 

public Integer getInvoices() { 
return invoices; 
} 

public void setInvoices(Integer invoices) { 
this.invoices = invoices; 
} 

public Double getInvValue() { 
return invValue; 
} 

public void setInvValue(Double invValue) { 
this.invValue = invValue; 
} 

public Integer getYetToFollow() { 
return yetToFollow; 
} 

public void setYetToFollow(Integer yetToFollow) { 
this.yetToFollow = yetToFollow; 
} 

} 
-----------------------------------com.example.OPDCReceive.java----------------------------------- 

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class OPDCReceive { 

@SerializedName("Customers") 
@Expose 
private Integer customers; 
@SerializedName("PDCs") 
@Expose 
private Integer pDCs; 
@SerializedName("UpTo7Days") 
@Expose 
private String upTo7Days; 
@SerializedName("PDCValue") 
@Expose 
private Double pDCValue; 

public Integer getCustomers() { 
return customers; 
} 

public void setCustomers(Integer customers) { 
this.customers = customers; 
} 

public Integer getPDCs() { 
return pDCs; 
} 

public void setPDCs(Integer pDCs) { 
this.pDCs = pDCs; 
} 

public String getUpTo7Days() { 
return upTo7Days; 
} 

public void setUpTo7Days(String upTo7Days) { 
this.upTo7Days = upTo7Days; 
} 

public Double getPDCValue() { 
return pDCValue; 
} 

public void setPDCValue(Double pDCValue) { 
this.pDCValue = pDCValue; 
} 

} 
-----------------------------------com.example.OPendingFollowup.java----------------------------------- 

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class OPendingFollowup { 

@SerializedName("Customers") 
@Expose 
private Integer customers; 
@SerializedName("Invoices") 
@Expose 
private Integer invoices; 
@SerializedName("PerRec") 
@Expose 
private Double perRec; 
@SerializedName("InvValue") 
@Expose 
private Double invValue; 

public Integer getCustomers() { 
return customers; 
} 

public void setCustomers(Integer customers) { 
this.customers = customers; 
} 

public Integer getInvoices() { 
return invoices; 
} 

public void setInvoices(Integer invoices) { 
this.invoices = invoices; 
} 

public Double getPerRec() { 
return perRec; 
} 

public void setPerRec(Double perRec) { 
this.perRec = perRec; 
} 

public Double getInvValue() { 
return invValue; 
} 

public void setInvValue(Double invValue) { 
this.invValue = invValue; 
} 

} 

轉換JSON到Java POJO使用下面的代碼

 private DashboardVOsList parseJsonResponse(JSONObject response) { 



    Gson gson =new Gson(); 
    DashboardVOsList dashboardVOsList= gson.fromJson(response.toString(),DashboardVOsList.class); 
return dashboardVOsList 


} 

您可以使用http://www.jsonschema2pojo.org/爲創建您的模型

+0

JSONObject jsonObject = response.getJSONObject(index);我在這行索引中收到錯誤。錯誤是:Json對象不能應用於int –

+0

我更新了代碼。請檢查 –

+0

我還需要所有的字段不僅lstGraph。 –

0

org.json。 JSONObject的不能轉換爲JSONArray

你應該叫的JSONObject,而不是JSONArray

不要

public void onResponse(JSONArray response) { 

public void onResponse(JSONObject response) { 

JSON

{ 「應收款」:1967560.60, 「應付款」:7508676.00」 oPendingFollowup 「:{」 流失客戶tomers 「:5,」 發票 「:7,」 PerRec 「:509.62,」 InvValue 「:10027000.00},」 oPDCReceive「:

private List parseJsonResponse(JSONObject response) 
{ 

    List<DashBoardVO> dashboardVOsList = null; 
    if (response != null) 
    { 
     dashboardVOsList = new ArrayList<>(); 
     DashBoardVO dashBoardVO = new DashBoardVO(); 

      for (int index = 0; index < response.length(); index++) 
       { 

       dashBoardVO.setReceivable(response.getDouble("Receivable")); 
       dashBoardVO.setPayable(response.getDouble("Payable")); 
       dashboardVOsList.add(dashBoardVO); 
       } 


    } 
     return dashboardVOsList; 
} 
+0

找到響應中的錯誤。錯誤是:無法應用mDashBoardVOsList = parseJsonResponse(response); –

+0

@RakeshKadadhi你的'response'是'JSONObject' –

+0

我做了同樣的代碼,但它沒有來 –