我一直在解析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"
}]
JAVA
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)
答案 0 :(得分: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
答案 1 :(得分: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"
}]
尝试使用不同的名词不要复杂。
答案 2 :(得分:0)
此示例显示如何解析json文本和图像。点击此处查看更多info
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();
}
}
}
答案 3 :(得分:0)
我不确定, 这会对你有所帮助
试试这个
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;