繁体   English   中英

卡在解析JSON中

[英]Stuck with parsing JSON

我一直无法解析JSON响应,通常我习惯于用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");

LOGCAT

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)

尝试发现差异:

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 : http://graph.facebook.com/timbillstrom/picture?width=500&height=500

我不确定,直到您发布堆栈跟踪。 尝试这个:

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"
}]

尝试使用不同的名词不要太复杂。

这个例子展示了如何解析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();
        }
    }
}

我不确定,这对您有帮助吗?

尝试这个

data = response.getJSONArray(null);
JSONObject json_user = data.getJSONObject(0);
 json_user.getString("0");
 json_user.getString("id");
 json_user.getString("1");
 json_user.getString("username");
 json_user.getString("3");
 json_user.getString("email_address");
 json_user.getString("4");
 json_user.getString("img");

并声明

JSONArray data = null;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM