从json格式数据中获取字符串

时间:2013-11-22 06:15:08

标签: android json

我正在尝试将json保存到数组列表中。

“响应”是字符串格式。

[
    {
        "code": "success"
    },
    {
        "a_updated": "Y",
        "a_id": "46155",
        "a_ref_annonceur": "LP-L-46155",
        "a_date_modified": "2013-11-21",
        "a_time_modified": "15:13:52",
        "a_titre_1": "Dans residence Pieds dans l’eau a Pointe aux Biches",
        "a_titre_2": "In residence on the beach in Pointe aux Biches"
    },
    {
        "a_updated": "N",
        "a_id": "46161",
        "a_ref_annonceur": "LP-L-46161",
        "a_date_modified": "2013-11-22",
        "a_time_modified": "09:27:31",
        "a_titre_1": "Appartement à Bain Boeuf de 3 chambres",
        "a_titre_2": "Bain Boeuf, 2 bedrooms appartment"
    },
    {
        "a_updated": "Y",
        "a_id": "47125",
        "a_ref_annonceur": "LP-A-47125",
        "a_date_modified": "2013-11-21",
        "a_time_modified": "15:14:09",
        "a_titre_1": "Terrain à route cotière, Mont Choisy avec batiment à renover.",
        "a_titre_2": "Mont Choisy For Sale land on coastal road"
    },
    {
        "a_updated": "Y",
        "a_id": "50196",
        "a_ref_annonceur": "LP-L-50196",
        "a_date_modified": "2013-11-12",
        "a_time_modified": "15:00:06",
        "a_titre_1": "Villa à Trou aux Biches face a la plage",
        "a_titre_2": "villa in Trou aux Biches infront of the beach"
    },
    {
        "a_updated": "Y",
        "a_id": "50197",
        "a_ref_annonceur": "LP-A-50197",
        "a_date_modified": "2013-11-12",
        "a_time_modified": "15:00:02",
        "a_titre_1": "Magnifique Villa de plain pied à Bon Espoir Piton sur terrain de 40 perches",
        "a_titre_2": "For sale magnificent villa at Bon Espoir Piton"
    },
    {
        "a_updated": "N",
        "a_id": "81435",
        "a_ref_annonceur": "LP-L-81435",
        "a_date_modified": "2013-11-22",
        "a_time_modified": "09:26:40",
        "a_titre_1": "Bungalow de 3 chambres dans une Residence a Péreybère",
        "a_titre_2": "Bungalow in a residence in Pereybere"
    },
    {
        "a_updated": "Y",
        "a_id": "93455",
        "a_ref_annonceur": "LP-A-93455",
        "a_date_modified": "2013-11-21",
        "a_time_modified": "15:15:04",
        "a_titre_1": "A vendre Grande Villa à Bain Boeuf",
        "a_titre_2": "Villa in Bain Boeuf of 5 bedrooms"
    },
    {
        "a_updated": "Y",
        "a_id": "100109",
        "a_ref_annonceur": "LP-A-100109",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "10:13:24",
        "a_titre_1": "A vendre villa de 5 chambres à Pointe aux Canonniers",
        "a_titre_2": "For sale 5 bedroom villa in Pointe aux Canonniers"
    },
    {
        "a_updated": "Y",
        "a_id": "101680",
        "a_ref_annonceur": "LP-L-101680",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "10:09:11",
        "a_titre_1": "Villa de 4 chambres en suite de grand standing à Belle Vue Harel",
        "a_titre_2": "4 bedrooms villa in Belle Vue Harel"
    },
    {
        "a_updated": "Y",
        "a_id": "101682",
        "a_ref_annonceur": "LP-L-101682",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "09:57:18",
        "a_titre_1": "Grande villa de 5 chambres + 1 study à Pointe aux Canonniers",
        "a_titre_2": "5 Bedrooms villa + a study in Pointe aux Canonniers"
    },
    {
        "a_updated": "Y",
        "a_id": "101898",
        "a_ref_annonceur": "LP-L-101898",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "10:07:27",
        "a_titre_1": "Villa meublée de 420M2 de 4 chambres + 1 bureau à Floréal",
        "a_titre_2": "Fully furnished villa of 420M2 in Floréal"
    },
    {
        "a_updated": "Y",
        "a_id": "102670",
        "a_ref_annonceur": "LP-A-102670",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "10:14:40",
        "a_titre_1": "villa sur terrain de 329,09M2 à Roches Noires",
        "a_titre_2": "Villa in Roches Noires"
    },
    {
        "a_updated": "Y",
        "a_id": "102918",
        "a_ref_annonceur": "LP-L-102918",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "10:02:35",
        "a_titre_1": "Villa climatissée à Pointe aux Canonniers",
        "a_titre_2": "Airconditioned villa in Pointe aux Canonniers"
    },
    {
        "a_updated": "Y",
        "a_id": "102922",
        "a_ref_annonceur": "LP-L-102922",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "09:55:50",
        "a_titre_1": "Magnifique villa à Belle vue Harel",
        "a_titre_2": "Magnificent villa in Belle Vue Harel"
    },
    {
        "a_updated": "Y",
        "a_id": "103940",
        "a_ref_annonceur": "LP-L-103940",
        "a_date_modified": "2013-10-17",
        "a_time_modified": "10:11:17",
        "a_titre_1": "Villa individuelle dans une résidence privé à Trou aux Biches",
        "a_titre_2": "Individual Villa in a private residence in Trou aux Biches"
    }
]

这是我的工作

尝试{

        JSONArray jObject=new JSONArray(response);

        for (int arr_size=0;arr_size<jObject.length();arr_size++){

            JSONObject dataObject= (JSONObject) jObject.get(arr_size);

            System.out.println("dataObject: "+dataObject.toString());
            a_updated = dataObject.getString("a_updated");

        }

我只想存储所有值

a_updated,A_ID,a_ref_annonceur,a_date_modified,a_time_modified,a_titre_1,a_titre_2

<\ n>在arraylist中

5 个答案:

答案 0 :(得分:6)

试试这个..

JSONArray jObject=new JSONArray(response);

        for (int arr_size=0;arr_size<jObject.length();arr_size++){

            JSONObject dataObject= jObject.getJSONObject(arr_size);

       if(dataObject.has("a_updated"))
            System.out.println("a_updated: "+dataObject.getString("a_updated"));
       if(dataObject.has("a_id"))
            System.out.println("a_id: "+dataObject.getString("a_id"));

            System.out.println("a_ref_annonceur: "+dataObject.getString("a_ref_annonceur"));
            System.out.println("a_date_modified: "+dataObject.getString("a_date_modified"));
            System.out.println("a_time_modified: "+dataObject.getString("a_time_modified"));
            System.out.println("a_titre_1: "+dataObject.getString("a_titre_1"));


        }

答案 1 :(得分:0)

而不是JSONObject dataObject= (JSONObject) jObject.get(arr_size);

JSONObject dataObject = jObject.getJSONObject(arr_size);

答案 2 :(得分:0)

试试这个......

    ArrayList<String> arrayList = new ArrayList<String>();
    try {
        JSONArray jsonArray = new JSONArray(response);
        for(int i =0,N = jsonArray.length();i<N;i++) {
            Object object = jsonArray.get(i);
            if(object instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) object;
                Iterator iterator = jsonObject.keys();
                while(iterator.hasNext()) {
                    String text = (String) iterator.next();
                    String string = jsonObject.getString(text);
                    arrayList.add(string);
                }
            }
        }
    } catch (Exception e) {
        // TODO: handle exception
    }

答案 3 :(得分:0)

private ArrayList<String> list = new ArrayList<String>

JSONArray jObject=new JSONArray(response);

    for (int arr_size=0;arr_size<jObject.length();arr_size++){

        JSONObject dataObject= jObject.getJSONObject(arr_size);
         list.add(dataObject.getString("a_updated"));
         different_array_list.add(dataObject.getString("object name your give here"));



    }

答案 4 :(得分:0)

//数组列表对象 ArrayList arrayList = new ArrayList();

//获取JSON数组的响应字符串 JSONArray jsonArray = new JSONArray(您的Response字符串);

    for (int i=1;i<jsonArray.length();i++){

        JSONObject dataObject= (JSONObject) jsonArray.get(i);
                            ;                           
            arrList.add(dataObject.getString("a_updated"));    
            arrList.add(dataObject.getString("a_id"));    
            arrList.add(dataObject.getString("a_ref_annonceur"));    
            arrList.add(dataObject.getString("a_date_modified"));    
            arrList.add(dataObject.getString("a_time_modified"));  
            arrList.add(dataObject.getString("a_titre_1"));    
            arrList.add(dataObject.getString("a_titre_2"));    
    }

//打印数组列表,您将获得所有数据

如果适合你,请重播我