Android解析2 jsonarray

时间:2016-04-23 12:15:34

标签: android arrays json parsing

我想解析2 JasonArray。我不使用listview,我使用微调器和文本框来显示数据。

这是我的信息

public class Config {
//JSON URL
public static final String DATA_URL = "http://bitirmeprojem.tk/data.php";

//Tags used in the JSON String
public static final String TAG_USERNAME = "Ders_Ad";
public static final String TAG_NAME = "Ders_Ad";
public static final String TAG_HOCA = "Hoca_Ad";
//public static final String TAG_SESSION = "session";

//JSON array name
public static final String JSON_ARRAY = "users";
public static final String JSON_ARRAY2 = "hoca";
}

这就是我解析它的地方:

private void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(Config.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(Config.JSON_ARRAY);

                        //Calling method getStudents to get the students   from the JSON Array
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

我尝试创建getData2(),但它崩溃了应用。

我还能做什么?

2 个答案:

答案 0 :(得分:0)

你的Api会给你这样的回复

{"users":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"}]}{"hoca":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"}]}

但实际对JSON格式有效,如此

{"users":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"}], "hoca":[{"Ders_Ad":"E-Ticaret"},{"Ders_Ad":"Yapay Zeka"},{"Ders_Ad":"Mant\u0131ksal Devre Tasar\u0131m\u0131"},{"Ders_Ad":"Programlama Dilleri"},{"Ders_Ad":"Bilgisayar Mimarisi"},{"Ders_Ad":"Veri \u0130leti\u015fimi"},{"Ders_Ad":"Bilgisayar A\u011flar\u0131"},{"Ders_Ad":"Veritaban\u0131 Sistemleri"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"Mustafa Cem","Hoca_Soyad":"Kasapba\u015f\u0131"},{"Hoca_Ad":"R\u0131fat","Hoca_Soyad":"Yaz\u0131c\u0131"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Halim","Hoca_Soyad":"Zaim"},{"Hoca_Ad":"Metin","Hoca_Soyad":"Turan"}]} 

答案 1 :(得分:0)

试试这个json Response。

Setup this valid json response in your API.

{
    "users": [{
        "Ders_Ad": "E-Ticaret"
    }, {
        "Ders_Ad": "Yapay Zeka"
    }, {
        "Ders_Ad": "Mant\u0131ksal Devre Tasar\u0131m\u0131"
    }, {
        "Ders_Ad": "Programlama Dilleri"
    }, {
        "Ders_Ad": "Bilgisayar Mimarisi"
    }, {
        "Ders_Ad": "Veri \u0130leti\u015fimi"
    }, {
        "Ders_Ad": "Bilgisayar A\u011flar\u0131"
    }, {
        "Ders_Ad": "Veritaban\u0131 Sistemleri"
    }],

    "hoca": [{
        "Ders_Ad": "E-Ticaret"
    }, {
        "Ders_Ad": "Yapay Zeka"
    }, {
        "Ders_Ad": "Mant\u0131ksal Devre Tasar\u0131m\u0131"
    }, {
        "Ders_Ad": "Programlama Dilleri"
    }, {
        "Ders_Ad": "Bilgisayar Mimarisi"
    }, {
        "Ders_Ad": "Veri \u0130leti\u015fimi"
    }, {
        "Ders_Ad": "Bilgisayar A\u011flar\u0131"
    }, {
        "Ders_Ad": "Veritaban\u0131 Sistemleri"
    }, {
        "Hoca_Ad": "R\u0131fat",
        "Hoca_Soyad": "Yaz\u0131c\u0131"
    }, {
        "Hoca_Ad": "Metin",
        "Hoca_Soyad": "Turan"
    }, {
        "Hoca_Ad": "Mustafa Cem",
        "Hoca_Soyad": "Kasapba\u015f\u0131"
    }, {
        "Hoca_Ad": "Mustafa Cem",
        "Hoca_Soyad": "Kasapba\u015f\u0131"
    }, {
        "Hoca_Ad": "R\u0131fat",
        "Hoca_Soyad": "Yaz\u0131c\u0131"
    }, {
        "Hoca_Ad": "Halim",
        "Hoca_Soyad": "Zaim"
    }, {
        "Hoca_Ad": "Halim",
        "Hoca_Soyad": "Zaim"
    }, {
        "Hoca_Ad": "Metin",
        "Hoca_Soyad": "Turan"
    }]
}