android中的Json解析类型不匹配错误

时间:2013-01-16 11:55:12

标签: android json

我正在尝试解析我的json数据

{
"000000000000000": [
    {
        "employee_boycode": "00",
        "id": "000",
        "address": "abcdef",
        "name": "name",
        "bankcode": "abc",
        "branch_name": "abcd",
        "account_no": "789"
    }
]
}

我的解析数据的代码是

JSONObject jsnJsonObject = new JSONObject(jsonResponse);//error coming here

contacts = jsnJsonObject.getJSONArray("000000000000000");

for (int i = 0; i < contacts.length(); i++)
{       
    JSONObject c = contacts.getJSONObject(i);
    id = c.getString("id");
    boy_code = c.getString("employee_boycode");
    name = c.getString("name");
    address = c.getString("address");
    branch_name = c.getString("branch_name");
    bankcode = c.getString("bankcode");
    account_no = c.getString(account_no);
}

我收到错误

01-17 12:51:41.280: V/response is(481): {"000000000000000":[{"employee_boycode":"012","id":"100","address":"sdfsfsdfsdf","name":"karan","bankcode":"HDFC","branch_name":"SARANGPUR","account_no":"100"}]} 
01-17 12:51:50.900: I/System.out(481): ServerEncodedResponse id 01-17 12:51:50.910: I/System.out(481): {"000000000000000":[{"employee_boycode":"012","id":"100","address":"sdfsfsdfsdf","name":"karan","bankcode":"HDFC","branch_name":"SARANGPUR","account_no":"100"}]}
 01-17 12:52:14.950: W/return(481): r2 
01-17 12:52:14.950: W/return(481): r3 
01-17 12:52:20.951: D/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol 01-17 
12:53:13.120: W/System.err(481): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
 01-17 12:53:13.210: D/dalvikvm(481): GC_FOR_MALLOC freed 3676 objects / 194656 bytes in 94ms
 01-17 12:53:13.220: W/System.err(481): at org.json.JSON.typeMismatch(JSON.java:107)
 01-17 12:53:13.220: W/System.err(481): at org.json.JSONObject.(JSONObject.java:158) 
01-17 12:53:13.300: W/System.err(481): at org.json.JSONObject.(JSONObject.java:171)

1 个答案:

答案 0 :(得分:2)

尝试使用此代码,

private void parseJson() {
    // TODO Auto-generated method stub
    String strJson="{\n\"000000000000000\": [\n    {\n        \"employee_boycode\": \"00\",\n        \"id\": \"000\",\n        \"address\": \"abcdef\",\n        \"name\": \"name\",\n        \"bankcode\": \"abc\",\n        \"branch_name\": \"abcd\",\n        \"account_no\": \"789\"\n    }\n]\n}\n";

    try {
    JSONObject jsnJsonObject = new JSONObject(strJson);


        JSONArray contacts = jsnJsonObject.getJSONArray("000000000000000");

        for (int i = 0; i < contacts.length(); i++)
        {       
            JSONObject c = contacts.getJSONObject(i);
            String id = c.getString("id");
            String boy_code = c.getString("employee_boycode");
            String name = c.getString("name");
            String address = c.getString("address");
            String branch_name = c.getString("branch_name");
            String bankcode = c.getString("bankcode");
            String account_no = c.getString("account_no");

            Log.e("Parsed data is",":"+id+" :"+boy_code+" :"+name+" :"+address+" :"+branch_name+" :"+bankcode+" :"+account_no);
        }

    } catch (JSONException e) {

        e.printStackTrace();
    }


}