0

通常は Java で JSON を解析するのに慣れていますが、今回は本当にクラッシュしてしまいました。誰かが何が悪いのか、どのように修正するのかを説明できますか?

JSON

[{
    "0": "1",
    "id": "1",
    "1": null,
    "name": null,
    "2": "tim",
    "username": "tim",
    "3": "tim@billstrom.me",
    "email_address": "tim@billstrom.me",
    "4": "http:\/\/graph.facebook.com\/timbillstrom\/picture?width=500&height=500",
    "img": "http:\/\/graph.facebook.com\/timbillstrom\/picture?width=500&height=500"
}]

ジャワ

JSONArray Jarray = new JSONArray(resJson);
String Jobject = Jarray.getJSONObject(0).getString("img");

ログキャット

09-15 20:15:36.637: W/System.err(9156): org.json.JSONException: Index 0 out of range [0..0)
09-15 20:15:36.677: W/System.err(9156):     at org.json.JSONArray.get(JSONArray.java:263)
09-15 20:15:36.677: W/System.err(9156):     at org.json.JSONArray.getJSONObject(JSONArray.java:480)
09-15 20:15:36.677: W/System.err(9156):     at com.christian.omegle.MainActivity$DownloadWebPageTask.onPostExecute(MainActivity.java:205)
09-15 20:15:36.687: W/System.err(9156):     at com.christian.omegle.MainActivity$DownloadWebPageTask.onPostExecute(MainActivity.java:1)
09-15 20:15:36.687: W/System.err(9156):     at android.os.AsyncTask.finish(AsyncTask.java:631)
09-15 20:15:36.687: W/System.err(9156):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-15 20:15:36.687: W/System.err(9156):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-15 20:15:36.687: W/System.err(9156):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 20:15:36.687: W/System.err(9156):     at android.os.Looper.loop(Looper.java:213)
09-15 20:15:36.687: W/System.err(9156):     at android.app.ActivityThread.main(ActivityThread.java:4787)
09-15 20:15:36.687: W/System.err(9156):     at java.lang.reflect.Method.invokeNative(Native Method)
09-15 20:15:36.687: W/System.err(9156):     at java.lang.reflect.Method.invoke(Method.java:511)
09-15 20:15:36.687: W/System.err(9156):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-15 20:15:36.687: W/System.err(9156):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-15 20:15:36.687: W/System.err(9156):     at dalvik.system.NativeStart.main(Native Method)
4

4 に答える 4

1

違いを見つけてみてください:

import org.json.JSONArray;
import org.json.JSONException;


public class JsonTest {

/**
 * @param args
 * @throws JSONException 
 */
public static void main(String[] args) throws JSONException {
    String resJson  = "[{\r\n" + 
            "    \"0\": \"1\",\r\n" + 
            "    \"id\": \"1\",\r\n" + 
            "    \"1\": null,\r\n" + 
            "    \"name\": null,\r\n" + 
            "    \"2\": \"tim\",\r\n" + 
            "    \"username\": \"tim\",\r\n" + 
            "    \"3\": \"tim@billstrom.me\",\r\n" + 
            "    \"email_address\": \"tim@billstrom.me\",\r\n" + 
            "    \"4\": \"http:\\/\\/graph.facebook.com\\/timbillstrom\\/picture?width=500&height=500\",\r\n" + 
            "    \"img\": \"http:\\/\\/graph.facebook.com\\/timbillstrom\\/picture?width=500&height=500\"\r\n" + 
            "}]";

    JSONArray Jarray = new JSONArray(resJson);
    String Jobject = Jarray.getJSONObject(0).getString("img");

    System.out.println(Jobject);
}
}

出力:http://graph.facebook.com/timbillstrom/picture?width=500&height=500

于 2013-09-15T18:23:23.130 に答える
0

この例は、json のテキストと画像を解析する方法を示しています。詳細については、ここをクリックしてください

package com.androidbegin.jsonparsetutorial;
 
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
 
public class MainActivity extends Activity {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);
        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }
 
    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {
 
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Android JSON Parse Tutorial");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }
 
        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
 
            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("worldpopulation");
 
                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("rank", jsonobject.getString("rank"));
                    map.put("country", jsonobject.getString("country"));
                    map.put("population", jsonobject.getString("population"));
                    map.put("flag", jsonobject.getString("flag"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
 
        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}
于 2013-09-15T18:23:01.470 に答える
0

スタック トレースを投稿するまではわかりません。これを試して:

String.valueOf(Jarray.getJSONObject(0).getString("img"));

オブジェクトの名前が 0 であると確信していますか?

"0" : [{
    "0": "1",
    "id": "1",
    "1": null,
    "name": null,
    "2": "tim",
    "username": "tim",
    "3": "tim@billstrom.me",
    "email_address": "tim@billstrom.me",
    "4": "http:\/\/graph.facebook.com\/timbillstrom\/picture?width=500&height=500",
    "img": "http:\/\/graph.facebook.com\/timbillstrom\/picture?width=500&height=500"
}]

複雑にならないように、さまざまな名詞を使用してみてください。

于 2013-09-15T18:17:54.233 に答える