不能在基本类型int上调用getjsonobject(int)

时间:2014-12-01 05:37:29

标签: android json primitive-types

有人可以帮我这个......

我只是想让这些代码行工作。在menuitemArray.length().getJSONObject(i),它给出了错误"cannot invoke getjsonobject(int) on the primitive type int"

以下是代码:

private void parse(TextView txtResult) throws Exception{
    jObject = new JSONObject(xResult);

    JSONArray menuitemArray = jObject.getJSONArray("message");
    String sret = "";
    for (int i = 0; i < menuitemArray.length(); i++){
        sret +=menuitemArray.length().getJSONObject(i).getString("username").toString()+" : ";

        System.out.println(menuitemArray.getJSONObject(i)
            .getString("username").toString());

        System.out.println(menuitemArray.getJSONObject(i)
            .getString("subject").toString());

        sret +=menuitemArray.getJSONObject(i)
            .getString("subject").toString()+"\n";
    }
    txtResult.setText(sret);
}

是的,我对此有点新意。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

更改

 sret +=menuitemArray.length().getJSONObject(i).getString("username").toString()+" : ";

 sret +=menuitemArray.getJSONObject(i).getString("username").toString()+" : ";

实际上 sret 是一个String变量。并且您试图从 menuitemArray 的长度中获取username

答案 1 :(得分:0)

试试这个:

  private void parse(TextView txtResult) throws Exception{
        jObject = new JSONObject(xResult);

        JSONArray menuitemArray = jObject.getJSONArray("message");
        String sret = "";
        for (int i = 0; i < menuitemArray.length(); i++){
            sret +=menuitemArray.getJSONObject(i).getString("username").toString()+" : ";

            System.out.println(menuitemArray.getJSONObject(i)
                .getString("username").toString());

            System.out.println(menuitemArray.getJSONObject(i)
                .getString("subject").toString());

            sret +=menuitemArray.getJSONObject(i)
                .getString("subject").toString()+"\n";
        }
        txtResult.setText(sret);
    }