如何得到json数组的总数?

时间:2016-06-14 12:55:31

标签: android

如何得到json数组的总数?我想在json文件中得到tolal no id我们可以看到有两个id。所以toltal no of count应该是两个。

总数不应该显示两个。因为在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": "Suryawanshi.govind@gmail.com",
"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": "ranasinghkaran@yahoo.com",
"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

1 个答案:

答案 0 :(得分: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。