java - Android解析Json错误

标签 java android json

我在 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/

相关文章:

java - 使用 Ebean 映射字符串和枚举集合(Play 2.0)

java - 碰撞 2 个矩形不起作用(边界框方法)

android - 更改 ImageView

java - 使用 JSON 将 2 个参数传递给 struts2 操作

json - Grails JSON字段丢失

javascript - 无法仅从通过 php 页面获取的 json 数据返回用户名

java - 数据访问对象原始类型与对象类型?

java - 为什么在构建Gradle项目时出现此错误?使用Gradle版本3.5

Android Studio - Android 12/API 31 模拟器中缺少 Google Play 商店应用

android - 显示软键盘后禁用粘性沉浸模式