[英]Android parsing Json error
我在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);
}
}
我開始此活動時遇到致命異常。 此代碼是有關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();
}
由於您的json響應具有嵌套數組,因此您需要這樣編寫代碼...
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++) {
subposts = posts.getJSONArray(i);
for (int j = 0; j < subposts.length(); j++) {
JSONObject c = subposts.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();
}
嘗試這個:
posts = json.getJSONArray(TAG_POST);
for (int i = 0; i < posts.getJSONArray(0).length(); i++) {
JSONObject c = posts.getJSONArray(0).getJSONObject(i);
String post = c.getString("0");
HashMap<String,String> map = new HashMap<String,String>();
map.put(TAG_POST, post);
postsList.add(map);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.