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

わかりませんが、「最初のセット」とはどういう意味ですか。mylistオブジェクトの最初の要素を意味しますか? (position = 0いつものように?)

「jsonarray」のサイズは正しいですか?

代わりに google gson ライブラリを使用してみてください。指定されたオブジェクト、またはこの場合はオブジェクトのリストにデータを解析していますが、何も変わらないと思います。

于 2012-09-11T08:06:52.340 に答える
-1

json の解析には AsyncTask を使用する必要があります。いくつかの解析リンクが役立ちます

リンク1

于 2012-09-11T07:42:53.330 に答える