2

我想将 JSON 转换为 java 代码。我的 jsoncode 如下所示。

{
"nodes": [
    {
        "node": {
            "Name": "rahul Patel",
            "Address": "\n\tAhmedabad",
            "Date of Birth": "1991-05-03",
            "Occupation": "developer",
            "Member Since": "3 weeks 4 days"
        }
    }
]

爪哇代码

try {
            JSONObject objResponse = new JSONObject(strResponse);

            JSONArray jsonnodes = objResponse
                    .getJSONArray(nodes);


            System.out.println("=hello this is DoinBackground");
            for (i = 0; i < jsonnodes.length(); i++) {

                System.out.println("hello this is for loop of DoinBackground");
                JSONObject jsonnode = jsonnodes.getJSONObject(i);

                JSONObject jsonnodevalue = jsonnode
                        .getJSONObject(node);

                bean = new UserProfileBean();

                bean.name = jsonnodevalue.getString(Name);


                listActivities.add(bean);

            }
        } catch (JSONException e) {

            e.printStackTrace();
        }
}

在 logcat 中,我打印了 for 循环之前的值System.out.println("=hello this is DoinBackground");,但值无法在 for 循环下打印System.out.println("hello this is for loop of DoinBackground");

注意:请让我知道,是否有可能我们不能在代码中使用 for 循环?如果是,那么给出解决方案,对于这个给定的问题还有另一种解决方案。

谢谢。

4

2 回答 2

1

尝试使用 Gson - http://code.google.com/p/google-gson/。会省去很多头痛。

但是,在您的代码中,请确保正确解析您的 JSON 字符串。只是为了确定objResponse.getJSONArray(nodes)应该是objResponse.getJSONArray("nodes")

于 2012-04-30T05:56:13.117 回答
1

你的 json 字符串是错误的。它必须以}. 解决这个问题,它会工作。

固定 json 字符串:

{
    "nodes": [
        {
            "node": {
                "Name": "rahul Patel",
                "Address": "\n\tAhmedabad",
                "Date of Birth": "1991-05-03",
                "Occupation": "developer",
                "Member Since": "3 weeks 4 days"
            }
        }
    ]
}

要测试的示例代码:

String j = "{\r\n" + 
        "    \"nodes\": [\r\n" + 
        "        {\r\n" + 
        "            \"node\": {\r\n" + 
        "                \"Name\": \"rahul Patel\",\r\n" + 
        "                \"Address\": \"\\n\\tAhmedabad\",\r\n" + 
        "                \"Date of Birth\": \"1991-05-03\",\r\n" + 
        "                \"Occupation\": \"developer\",\r\n" + 
        "                \"Member Since\": \"3 weeks 4 days\"\r\n" + 
        "            }\r\n" + 
        "        }\r\n" + 
        "    ]\r\n" + 
        "}";

try{
    JSONObject objResponse = new JSONObject(j);

    JSONArray jsonnodes = objResponse.getJSONArray("nodes" );

    for (int i = 0; i < jsonnodes.length(); i++) {

        JSONObject jsonnode = jsonnodes.getJSONObject(i);

        JSONObject jsonnodevalue = jsonnode
                .getJSONObject("node");

        Log.v("name", jsonnodevalue.getString("Name"));
        Log.v("address", jsonnodevalue.getString("Address"));
        Log.v("occupation", jsonnodevalue.getString("Occupation"));
    }

}
catch (Exception e) {
    e.printStackTrace();
}
于 2012-04-30T06:00:05.523 回答