2

I'm trying to parse my json data

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

my code for parsing the data is

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

and i'm getting error

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)
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36
Raj
  • 844
  • 2
  • 11
  • 29
  • You can refer http://stackoverflow.com/questions/13425244/java-lang-string-cannot-be-converted-to-jsonobject – user1969053 Jan 16 '13 at 11:59
  • try introducing quotes: {"000000000000000": " [{"employee... ]" } – Alpay Jan 16 '13 at 12:00
  • check http://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject – Nermeen Jan 16 '13 at 12:02
  • before the error statement try to print log like this Log.v("response is",jsonResponse.toString()); and postback the log in your question – TNR Jan 16 '13 at 12:08
  • i think your missing " ", perform like this account_no = c.getString("account_no"); – Rahul Baradia Jan 16 '13 at 12:16

1 Answers1

2

try this code it should work,

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


}
dd619
  • 5,910
  • 8
  • 35
  • 60