无法从JsonArray获取String

时间:2016-03-15 10:23:48

标签: android json arraylist

我在数组中得到2,3& 4字符串(值),但我无法在textview中设置值。我在textview中只得到4个值也是相同的repeat。请告诉我如何处理这个。下面是JSON:

    {
    results: [
    {
    types: [
    "sublocality_level_3",
    "sublocality",
    "political"
    ]
    },
    {
    types: [
    "point_of_interest",
    "establishment"
    ]
    },
    {
    types: [
    "travel_agency",
    "point_of_interest",
    "establishment"
    ]
    },
    {
    types: [
    "point_of_interest",
    "establishment"
    ]
    },
    {
    types: [
    "store",
    "point_of_interest",
    "establishment"
    ]
    },
    {
    types: [
    "dentist",
    "health",
    "point_of_interest",
    "establishment"
    ]
    },
    {
    types: [
    "clothing_store",
    "store",
    "point_of_interest",
    "establishment"
    ]
    },
    {
    types: [
    "point_of_interest",
    "establishment"
    ]
    },
    ]
    }

编码我正在尝试的内容:

    try {
        object = new JSONObject(url);
        JSONArray array = object.getJSONArray("results");
        for (int i = 0; i < array.length(); i++){
            JSONObject object1 = array.getJSONObject(i);
            JSONArray array1 = object1.getJSONArray("types");
            for(int j = 0; j < array1.length(); j++){
                if (array1.length()==2) {
                    t1 = array1.getString(0);
                    t2 = array1.getString(1);
                }  if (array1.length()==3) {
                    t1 = array1.getString(0);
                    t2 = array1.getString(1);
                    t3 = array1.getString(2);
                }  if (array1.length()==4) {
                    t1 = array1.getString(0);
                    t2 = array1.getString(1);
                    t3 = array1.getString(2);
                    t4 = array1.getString(3);
                }
            }

            getterSetter.setT1(t1);
            getterSetter.setT2(t2);
            getterSetter.setT3(t3);
            getterSetter.setT4(t4);
            list.add(getterSetter);
            adapter = new GooglePlacesAdapter(GooglePlace.this,list);
            listView.setAdapter(adapter);
            }

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

2 个答案:

答案 0 :(得分:0)

你应该有一个与此类似的行

GetterSetter getterSetter = new GetterSetter();

将此行移到第一个for循环中。

注意:完全不需要内部for循环(2nd)。您可以删除该循环并将if语句保留在其中。

更新:您的代码应该像

try {
    object = new JSONObject(url);
    JSONArray array = object.getJSONArray("results");
    for (int i = 0; i < array.length(); i++){
        JSONObject object1 = array.getJSONObject(i);
        JSONArray array1 = object1.getJSONArray("types");
        GetterSetter getterSetter = new GetterSetter(); // This line should be inside for loop
        if (array1.length()==2) {
            getterSetter.setT1(array1.getString(0));
            getterSetter.setT2(array1.getString(1));
        } else if (array1.length()==3) {
            getterSetter.setT1(array1.getString(0));
            getterSetter.setT2(array1.getString(1));
            getterSetter.setT3(array1.getString(2));
        } else if (array1.length()==4) {
            getterSetter.setT1(array1.getString(0));
            getterSetter.setT2(array1.getString(1));
            getterSetter.setT3(array1.getString(2));
            getterSetter.setT4(array1.getString(3));
        }

        list.add(getterSetter);
        adapter = new GooglePlacesAdapter(GooglePlace.this,list);
        listView.setAdapter(adapter);
    }

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

答案 1 :(得分:-1)

fIRST数组格式不应该是语法

{
 "results": [
 {
   "types": [
  "sublocality_level_3",
"sublocality",
"political"
]
},
{
"types": [
"point_of_interest",
"establishment"
]
},
{
"types": [
"travel_agency",
"point_of_interest",
"establishment"
]
},
{
"types": [
"point_of_interest",
"establishment"
]
},
{
"types": [
"store",
"point_of_interest",
"establishment"
]
},
{
"types": [
"dentist",
"health",
"point_of_interest",
"establishment"
]
},
{
"types": [
"clothing_store",
"store",
"point_of_interest",
"establishment"
]
},
{
"types": [
"point_of_interest",
"establishment"
]
}
]
}

在代码中你这样做

try {
    object = new JSONObject(url);
    JSONArray array = object.getJSONArray("results");
    for (int i = 0; i < array.length(); i++){
        JSONObject object1 = array.getJSONObject(i);
        JSONArray array1 = object1.getJSONArray("types");
        for(int j = 0; j < array1.length(); j++){
            if (array1.length()==2) {
                t1 = array1.getString(0);
                t2 = array1.getString(1);
                t3 = "";
                t4 = "";
            }  else if (array1.length()==3) {
                t1 = array1.getString(0);
                t2 = array1.getString(1);
                t3 = array1.getString(2);
                t3 = "";
                t4 = "";
            }  else  if (array1.length()==4) {
                t1 = array1.getString(0);
                t2 = array1.getString(1);
                t3 = array1.getString(2);
                t4 = array1.getString(3);

            }
           getterSetter.setT1(t1);
           getterSetter.setT2(t2);
           getterSetter.setT3(t3);
           getterSetter.setT4(t4);
           list.add(getterSetter);
        }

        adapter = new GooglePlacesAdapter(GooglePlace.this,list);
        listView.setAdapter(adapter);
        }

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

希望这对你有帮助