Json Object为null

时间:2012-03-19 19:53:35

标签: java android json

我有一个名为JSONParse的基于JsonObject的类。有这种方法:

public String getName()
    {
        try {
            JSONObject jobj = this.getJSONObject("Data");
            Log.e("NFF NAME", jobj.toString());
            JSONObject jobj2= jobj.getJSONObject("User");
            Log.e("NFF NAME", jobj2.toString());
            return jobj2.getString("username");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

日志:

03-19 19:50:09.280: E/NFF NAME(5909): {"User":{"picture":"http://vm19.htl-leonding.ac.at/img/30.png","gender":"f","username":"lisa","age":32}} 
03-19 19:50:09.280: E/NFF NAME(5909): {"picture":"http://vm19.htl-leonding.ac.at/img/30.png","gender":"f","username":"lisa","age":32}

问题:

上面的方法总是为名称返回null。

请帮助

1 个答案:

答案 0 :(得分:0)

用户名在用户内...试试这个

public String getName()
    {
        try {
            JSONObject jobj = this.getJSONObject("Data");
            Log.e("NFF NAME", jobj.toString());
            JSONObject jobj2= jobj.getJSONObject("User");
            Log.e("NFF NAME", jobj2.toString());
            JSONObject jobj2b= jobj2.getJSONObject("User");
            Log.e("NFF NAME", jobj2b.toString());
            return jobj2b.getString("username");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

我没有测试,但你可以试试......

<强> EDITED : 对不起,我写得快,不看代码,jobj2b指向jobj2第一个孩子:'用户'