来自其他类的JSON响应

时间:2016-11-22 05:16:11

标签: java android json android-volley

我有班级名称

public class DriverLocation

{


    public static String[] driverName;
    public static String[] driverCarName;
    public static String[] driverCarModel;
    public static double[] driverLat;
    public static double[] driverLang;

    private JSONArray drivers = null;

    private String json;

    public String URL_ITEMS = "http://steam.com.pk/Nexer/driver.php";
    private static final String TAG_Table_Name = "Driver";
    private static final String TAG_Driver_Name = "driverName";
    private static final String TAG_Driver_Car_Name ="driverCarName";
    private static final String TAG_Driver_Car_Modle = "driverCarModel";
    private static final String TAG_Driver_Lat = "driverLat";
    private static final String TAG_Driver_Lang = "driverLang";

    public DriverLocation(String json) {
        this.json = json;
    }
    public void parJson(){


        JSONObject jsonObject=null;



        try {
            jsonObject=new JSONObject(json);
            drivers=jsonObject.getJSONArray(TAG_Table_Name);

            driverName = new String[drivers.length()];
            driverCarName = new String[drivers.length()];
            driverCarModel = new String[drivers.length()];
            driverLat = new double[drivers.length()];
            driverLang = new double[drivers.length()];


            for (int i=0;i<drivers.length();i++){
                JSONObject jo=drivers.getJSONObject(i);



                driverName[i]=jo.getString(TAG_Driver_Name);
                driverCarName[i]=jo.getString(TAG_Driver_Car_Name);
                driverCarModel[i]=jo.getString(TAG_Driver_Car_Modle);
                driverLat[i]= jo.getDouble(TAG_Driver_Lat);
                driverLang[i]= jo.getDouble(TAG_Driver_Lang);

            }


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

和片段活动,我在响应中使用volley库我收到json数组,但我不知道如何在此活动中从该数组中获取值是我从DriverLocation类获取值的代码

private void showJSON(String json){
        DriverLocation pj = new DriverLocation(json);
        pj.parJson();

      /*  double[] array={Double.parseDouble("23423.234"), Double.parseDouble("23423.234")};
        double[] array1={Double.parseDouble("23423.234"), Double.parseDouble("23423.234")};
        double[] array2={23423.234,23423.234};
        for (int i=0;i<=array2.length;i++){



        }*/
        for (int i=0;i<=DriverLocation.driverName.length;i++){



        }
参数String json中的

我从排球响应listerner传递我的响应 当我只是打印长度我收到正确的长度,但当我想从其他类打印driverName时,它不会返回我的名字。 。

my Json

{"Driver":[{"driverName":"Naveed","driverCarName":"Honda City","driverCarModel":"2008","driverLat":"33.999253","driverLang ":"71.544465"},{"driverName":"Naveed","driverCarName":"Honda City","driverCarModel":"2008","driverLat":"33.999253","driverLang ":"71.544465"},{"driverName":"Naveed","driverCarName":"Honda City","driverCarModel":"2008","driverLat":"33.999253","driverLang ":"71.544465"}]}

1 个答案:

答案 0 :(得分:1)

虽然我建议你研究一下Retrofit(或者至少是Gson),而不是使用静态变量,并将你的JSON解析成一些自定义Java类......等等......

private void showJSON(String json){
    DriverLocation pj = new DriverLocation(json); // This does something with 
    pj.parJson(); // This builds some static arrays

    // This loops over an array
    for (int i=0;i<=DriverLocation.driverName.length;i++){
        String name = DriverLocation.driverName[i]; // Gets value
    }

现在,当然,您必须循环(或直接访问)所有其他阵列。

这就是Java类更好的原因,例如Driver[]

for (Driver d : DriverLocation.drivers){
    String name = d.getName(); 
}

这一切都是读者的一项练习。