0

我已经解析了一个 JSON 文件,然后将一个数组从 JSON 保存到一个字符串,并将其传递给下一个要解析的活动,但我有一个问题,即列表视图只显示第一组位置而不显示任何其他位置。

JSON 片段:

{
title: World Rover Scout Moot
locations: [
{
address: Kandersteg, Switzerland
latitude: 46.483742
longitude: 7.663157
name: 01st World Rover Moot (1921)
}
{
address: Ingarö, Sweden
latitude: 59.25235
longitude: 18.465536
name: 02nd World Rover Moot (1925)
}
{
address: Monzie, Scotland
latitude: 56.404182
longitude: -3.818855
name: 03rd World Rover Moot (1939)
}
{
address: Skjak, Norway
latitude: 61.898525
longitude: 8.369957
name: 04th World Rover Moot (1949)
}
{
address: Kandersteg, Switzerland
latitude: 46.483742
longitude: 7.663157
name: 05th World Rover Moot (1953)
}
{
address: Sutton Coldfield, UK
latitude: 52.558769
longitude: -1.802851
name: 06th World Rover Moot (1957)
}
{
address: Melbourne, Australia
latitude: -37.814251
longitude: 144.963165
name: 07th World Rover Moot (1961)
}
{
address: Melbourne, Australia
latitude: -37.814251
longitude: 144.963165
name: 08th World Rover Moot (1990-91)
}
{
address: Kandersteg, Switzerland
latitude: 46.483669
longitude: 7.663095
name: 09th World Rover Moot (1953)
}
{
address: Ransberg, Sweden
latitude: 58.454601
longitude: 14.226012
name: 10th World Moot (1996)
}
{ 
address: Mexico
latitude: 19.691061
longitude: -98.805557
name: 11th World Scout Moot (2000)
}
{
address: Hualien, Taiwan
latitude: 23.987925
longitude: 121.612175
name: 12th World Scout Moot (2004)
}
{
address: Nairobi, Kenya
latitude: -1.312558
longitude: 36.769932
name: 13th World Scout Moot
}
]
},

{
title: World Scout Interreligious Symposium
locations: [
{
address: Kampala, Uganda
latitude: 0.239059
longitude: 32.623615
name: 3rd World Scout Interreligious Symposium
}
]
}

源代码:

public class SingleMapSet  extends Activity {
final String KEY_MAPS = "maps";

static final String KEY_MAPSETMAPSET = "";
static final String KEY_MAPNAME = "name";
static final String KEY_MAPLOCATION = "address";
static final String KEY_MAPLONG = "longitude";
static final String KEY_MAPLAT = "latitude";
ListView list;
org.scouts.android.maps.MapsetAdapter adapter;


/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlists);
    TextView lblTitle = (TextView) findViewById(R.id.actionbar);
    lblTitle.setText(R.string.maps);

    Intent in = getIntent();
    String mapset = in.getStringExtra(KEY_MAPSETMAPSET);
    mapset = mapset.substring(1);
    mapset = mapset.substring(0, mapset.length() - 1);



    final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


    String jsonString = null;
    try {
        jsonString = mapset;
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    JSONObject json = null;
    try {
        json = new JSONObject(jsonString);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

           try{




               JSONArray  jsonArray = json.getJSONArray("locations");
               for(int i=0;i < jsonArray.length();i++){                     


            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject maps = jsonArray.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put(KEY_MAPNAME, maps.getString(KEY_MAPNAME));
            map.put(KEY_MAPLOCATION, maps.getString(KEY_MAPLOCATION));
            map.put(KEY_MAPLONG, maps.getString("longitude"));
            map.put(KEY_MAPLAT, maps.getString("latitude"));

            mylist.add(map);


        }
           }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
           }
           list=(ListView)findViewById(R.id.playlistlist);          
           adapter=new org.scouts.android.maps.MapsetAdapter (this, mylist);        
           list.setAdapter(adapter);
           list.setOnItemClickListener(new OnItemClickListener() {

               public void onItemClick(AdapterView<?> parent, View view, int position,
                       long id) {

                   HashMap<String, String> map = mylist.get(position);

                   Intent in = new Intent(SingleMapSet.this, org.scouts.android.maps.SingleMapSet.class);
                   in.putExtra(KEY_MAPNAME, map.get(KEY_MAPNAME));
                   in.putExtra(KEY_MAPLOCATION, map.get(KEY_MAPLOCATION));
                   in.putExtra(KEY_MAPLONG, map.get(KEY_MAPLONG));
                   in.putExtra(KEY_MAPLAT, map.get(KEY_MAPLAT));

                startActivity(in);


            }
        });
    }








                };      ;
4

4 回答 4

1

首先删除这一行 mapset = mapset.substring(1); mapset = mapset.substring(0, mapset.length() - 1);

并尝试这种方式

 JSONObject jOBj;
 JSONArray  jsonArray;
 JSONArray jarray=new JSONArray(mapset);

 for(int j=0;j<jarray.length();j++){
       jOBj=jarray.getJSONObject(j)
       jsonArray = jOBj.getJSONArray("locations");
         for(int i=0;i < jsonArray.length();i++){                     


           HashMap<String, String> map = new HashMap<String, String>();
           JSONObject maps = jsonArray.getJSONObject(i);

           map.put("id",  String.valueOf(i));
           map.put(KEY_MAPNAME, maps.getString(KEY_MAPNAME));
           map.put(KEY_MAPLOCATION, maps.getString(KEY_MAPLOCATION));
           map.put(KEY_MAPLONG, maps.getString("longitude"));
           map.put(KEY_MAPLAT, maps.getString("latitude"));

           mylist.add(map);


    }


 }
于 2012-09-11T09:27:18.903 回答
0

首先,您的 JSON 中似乎缺少某些内容。它似乎是一个数组,但 [] 不存在。(它是你在开始时用 2 个子字符串删除的吗?)

然后,您可能会做的是:

JSONArray json = new JSONArray(jsonString); // Note that it is supposed to be an Array

进而

for(int j=0;j < json.length();j++){
    JSONArray jsonArray = json.getJSONObject(j).getJSONArray("location");
    // From here proceed as you did
于 2012-09-11T08:05:20.770 回答
-1

您必须使用 AsyncTask 来解析 json。有一些解析链接可以帮助你

链接1

于 2012-09-11T07:42:53.330 回答
-1

我不知道,你说的“第一组”是什么意思。您是指mylist对象的第一个元素吗?(就像position = 0所有的时间一样?)

“jsonarray”的大小是否正确?

您可以尝试改用 google gson 库。它将数据解析为指定的对象,或者在这种情况下,解析为对象列表,但我认为它不会改变任何东西。

于 2012-09-11T08:06:52.340 回答