0

私の Android アプリでは、Google Places URL からの JSON データを ListView に解析しようとしています。近くのレストランの名前をリストしようとしています。解析は機能しているように見えますが、JSON データから "name" 変数を取得する方法がわかりません。返されたリストには、次のような項目が含まれています。

    {"id":
"6217c344be105e............"
{"id":
"2c66bf813799zr2844......"

私には、Google Place URL の「results」配列の「id」変数を解析しているように見えます。「結果」配列の「名前」変数を取得したい。誰かがこれを行うための正しいコードを教えてもらえますか? 私が使用しているJSON解析方法は次のとおりです。

    public void parseJSON() throws ClientProtocolException, IOException, JSONException
{
    String bcURL="https://maps.googleapis.com/maps/api/place/search/json?"
        + "location=" + latString + "," + longiString
        + "&radius=15000&"
        + "types=restaurant&sensor=false&key="
        + myPlaceKey;

    //--- Get Places URL ----
    client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet(bcURL));
    int statusLine = response.getStatusLine().getStatusCode();
    if(statusLine == 200){
        HttpEntity e = response.getEntity();
        String data = EntityUtils.toString(e);
        JSONObject urlData = new JSONObject(data);
        JSONArray resultsArr = urlData.getJSONArray("results");
        int length = resultsArr.length();
        List<String> listContents = new ArrayList<String>(length);
        for (int i = 0; i < length; i++)
        {
            listContents.add(resultsArr.getString(i));
        }

        ListView restListView = (ListView) findViewById(R.id.jsonList);
        restListView.setAdapter(new ArrayAdapter<String>(this,                      android.R.layout.simple_list_item_1, listContents));

    }
    //--- END Get Places URL ----

およびコードのリストビュー部分:

            JSONArray resultsArr = urlData.getJSONArray("results");
        int length = resultsArr.length();
        List<String> listContents = new ArrayList<String>(length);
        for (int i = 0; i < length; i++)
        {
            listContents.add(resultsArr.getString(i));
        }

        ListView restListView = (ListView) findViewById(R.id.jsonList);
        restListView.setAdapter(new ArrayAdapter<String>(this,                      android.R.layout.simple_list_item_1, listContents));

    }
    //--- END Get Places URL ----

コードの「名前変数を取得する」ビットはどこに行き、そのコードはどのようになりますか?

4

2 に答える 2

0

JSONファイルの最初のオブジェクトを取得する場合は、次のようにする必要があります。

        JSONArray samplearr = null;
        samplearr = new JSONArray(String);
       for(int index=0;index<samplearr.length();index++)
      {
        try
        {   
          JSONObject obj = samplearr.getJSONObject(index);

          JSONObject userobj = samplearr.getJSONObject(index).getJSONObject("user");

          Log.d("object","sample array created");
           userobj.getString("screen_name"),
          obj.get("text").toString(),
          userobj.getString("profile_image_url")

      }catch(Exception er)
      {
          Log.d("exceptiom","exception found at : "+er.getMessage());
      }

      }

基本的に、オブジェクト内のオブジェクトを取得する場合は、それを実行します。あなたの質問によると、あなたが望むフィールドは「id」フィールドの中にあるようですので、それを2回解析する必要があります

于 2012-07-30T16:05:24.683 に答える
0

hereを見てください。必要なものがすべて説明されているはずです。

簡単に言えば、jsonObjectから必要な値を取得する必要がありますJSONObject.getString("attributeName")

于 2012-07-30T15:44:09.123 に答える