繁体   English   中英

Android解析Json错误

[英]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.

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