2016-06-14 178 views
-4

如何獲得json數組的總數no?我想在json文件中獲得tolal no的id,我們可以看到有兩個id。因此,不要數兩個應該是兩個。如何獲得json數組的總數?

總數不應該顯示兩個。因爲在json中存在兩個id。我們怎麼能得到這個數字? 在圖像中看到我得到整個JSON。 但是我想要點擊「引導」按鈕後,只有兩個「id」的總數沒有。 lead.json

[ 
     { 
       "id": "449876", 
       "First Name": "Govind", 
"Middle Name" :"Shripatrao", 
"Last Name":"Suryawanshi", 
"City":"Gurgaon", 
"Country":"India", 
"Contact No":"+91 8586925935", 
       "email": "[email protected]", 
"Budget":"Starting at ?1.7 Crores onwards", 
"Project":" -", 
"App Platform":"UtilityApp-Android", 
"Source":"Organic", 
"Campaign":"NA", 
"Lead Time":"11/8/2015 2:51:32", 
"IP Address":"182.64.13.180" 

     }, 
     { 
"id": "425676", 
       "First Name": "Karan", 
"Middle Name" :"Singh", 
"Last Name":"Rana", 
"City":"Chandigarh", 
"Country":"India", 
"Contact No":"+91 9854563132", 
       "email": "[email protected]", 
"Budget":"Starting at ?3.35 Crore onwards", 
"Project":" Myst", 
"App Platform":"UtilityApp-Android", 
"Source":"Organic", 
"Campaign":"NA", 
"Lead Time":"9/15/2015 12:05:28", 
"IP Address":"182.71.22.178" 

       } 

    ] 

主要活動

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonArrayRequest; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import tatahousingleads.com.tatahousingleads.app.AppController; 


public class MainActivity extends Activity { 



    // json array response url 
    private String urlJsonArry = "http://milagro.in/wip/apps/n/lead.json"; 

    private static String TAG = MainActivity.class.getSimpleName(); 
    private Button btnMakeObjectRequest, btnMakeArrayRequest; 

    // Progress dialog 
    private ProgressDialog pDialog; 

    private TextView txtResponse; 

    // temporary string to show the parsed response 
    private String jsonResponse; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest); 
     btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest); 
     txtResponse = (TextView) findViewById(R.id.txtResponse); 

     pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 



     btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // making json array request 
       makeJsonArrayRequest(); 
      } 
     }); 

    } 







    private void makeJsonArrayRequest() { 

     showpDialog(); 

     JsonArrayRequest req = new JsonArrayRequest(urlJsonArry, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 

         try { 
          // Parsing json array response 
          // loop through each json object 
          jsonResponse = ""; 
          for (int i = 0; i < response.length(); i++) { 

           JSONObject lead = (JSONObject) response 
             .get(i); 

           String id = lead.getString("id"); 
           String firstname = lead.getString("First Name"); 

           String middname = lead.getString("Middle Name"); 
           String lanme = lead.getString("Last Name"); 
           String city =lead.getString("City"); 
           String country =lead.getString("Country"); 
           String contactno =lead.getString("Contact No"); 
           String email = lead.getString("email"); 
           String budget = lead.getString("Budget"); 
           String project = lead.getString("Project"); 
           String appplatform = lead.getString("App Platform"); 


           jsonResponse += "Id: " + id + "\n\n"; 
           jsonResponse += "First Name: " +firstname + "\n\n"; 
           jsonResponse += "Middle Name: " + middname + "\n\n"; 
           jsonResponse += "Last Name: " + lanme + "\n\n"; 
           jsonResponse += "City: " + city + "\n\n"; 
           jsonResponse += "Country: " + country + "\n\n"; 
           jsonResponse += "Contact No: " + contactno + "\n\n"; 
           jsonResponse += "Email: " + email + "\n\n"; 
           jsonResponse += "Budget: " + budget + "\n\n"; 
           jsonResponse += "Project: " + project + "\n\n"; 
           jsonResponse += "App Platform : " + appplatform + "\n\n\n"; 

          } 

          txtResponse.setText(jsonResponse); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
          Toast.makeText(getApplicationContext(), 
            "Error: " + e.getMessage(), 
            Toast.LENGTH_LONG).show(); 
         } 

         hidepDialog(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_SHORT).show(); 
       hidepDialog(); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(req); 
    } 

    private void showpDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hidepDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

activity_main.xml中

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:scrollbarStyle="insideInset" 
    android:scrollbars="vertical" > 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 



    <Button 
     android:id="@+id/btnArrayRequest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="50dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:text="Leads" 
     /> 

    <TextView 
     android:id="@+id/txtResponse" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/btnArrayRequest" 
     android:layout_marginTop="20px" 
     android:padding="20dp" /> 

</LinearLayout> 
    </ScrollView> 

image

+2

JsonArray.length()會給你JsonArray的長度。 –

+0

請確定。解釋你真正需要的。您已經使用response.length()來獲取長度。 – Ayyappan

+0

response.length()所有準備好給你。 – anonymous

回答

1

在你MainActivity.java類,方法makeJsonArrayRequest()的內部,在OnResponse回調方法,你已經得到了數組的大小。看看你的for循環,

   @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 

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

         // Your code 

         } 

         Log.e("Array Length", response.length()); // Here you will get the length.        

響應哪裏是你的JSONArray。

+0

請在這裏看到json文件,你可以看到「id」只出現兩次。 &我想要單擊Leads按鈕後,它應該是隻顯示兩個ID的計數。 目前我點擊「Leads」按鈕後會得到整個json。 – user6313669

+0

將數組長度存儲在'int'變量中,然後將其打印在Button上單擊 – Nayan

+1

這是因爲您在單擊Leads Leads按鈕時調用Web服務。 – SripadRaj