2

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)
4

1 に答える 1

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();
    }


}
于 2013-01-16T12:25:14.330 に答える