我在 Android 应用程序中解析 json 数组时遇到错误。
我的 json 对象采用以下形式:
{"post":[
[{"0":"all the best to every one...!!","post":"all the best to every one...!!"},
{"0":"hello every one...","post":"hello every one..."}]
]}
我在android中的java文件如下:
public class Newsactivity extends ListActivity {
private static final String TAG_SUCCESS = "";
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> postsList;
private static String url_all_products = "myurl";
private static final String TAG_POST = "post";
JSONArray posts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allposts);
postsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Newsactivity.this);
pDialog.setMessage("Loading posts. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
Log.d("All Posts: ", json.toString());
try {
posts = json.getJSONArray(TAG_POST);
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
String post = c.getString(TAG_POST);
HashMap<String,String> map = new HashMap<String,String>();
map.put(TAG_POST, post);
postsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
Newsactivity.this, postsList,
R.layout.singlepost, new String[] { TAG_POST},
new int[] { R.id.pid});
setListAdapter(adapter);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_newsactivity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我在开始此 Activity 时遇到致命异常。这段代码是网上json解析教程的修改代码。由于我是 android 新手,我无法找到 id 在哪里。所以,请帮助我。谢谢。
最佳答案
根据您的代码,我担心您的 json 格式“有点奇怪”(或者我可能会说它与您的代码试图执行的格式不同)。 正如你所看到的,第一个“post”后面有两个“[”,这意味着元素数组实际上位于另一个数组内。
{"post":[ // first '['
[ // second '['
{"0":"all the best to every one...!!","post":"all the best to every one...!!"},
{"0":"hello every one...","post":"hello every one..."}
]
]}
为了使其正确,请执行以下操作
try {
posts = json.getJSONArray(TAG_POST);
posts = posts.getJSONArray(0) // to get the first array
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
String post = c.getString(TAG_POST);
HashMap<String,String> map = new HashMap<String,String>();
map.put(TAG_POST, post);
postsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
关于java - Android解析Json错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954499/