1

Here is my JSON

 {
  "keys": [
    "10_Ultimate",
    "20_Yemensoft"
  ]
}

When I hit directly in the Url I am getting correct response but not from Android. Also I have seen in jsonlint the JSON format is correct. But I am getting following message:

org.json.JSONException: Unterminated array at character 11 of [B@41c16df8

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Maqsood Alam
  • 27
  • 1
  • 7
  • say the url that get json from it. – Ahmad Vatani Aug 03 '15 at 07:58
  • client.get("http://192.168.1.15:8080/AndroidRest/financial/finList", new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) { JSONObject jsnObj = new JSONObject(); String res = response.toString(); – Maqsood Alam Aug 03 '15 at 08:26
  • 'http://192.168.1.15:8080/AndroidRest/financial/finList' this url is on your localhost of pc. you cant access this on your phone! select an url on internet! – Ahmad Vatani Aug 03 '15 at 08:39
  • 1
    Possible duplicate http://stackoverflow.com/questions/25771184/parsing-json-from-url-with-exception-error-parsing-data-org-json-jsonexception – Ganesh Kumar Aug 03 '15 at 08:41
  • gettinr error at JsonObject jso = new JsonObject(res); – Maqsood Alam Aug 03 '15 at 10:40

2 Answers2

0

when i hit the Url http://192.168.0.103:8080/AndroidRest/financial/finList

I am getting correct json response Also i validated in jsonlint.com

{
  "keys" : [ "10_Ultimate", "20_Yemensoft" ]
}

when i try to deserialize from android it saysorg.json.JSONException: Unterminated array at character 11

 @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                  JSONObject jsnObj  = new JSONObject();
                  String res = response.toString();
                try {
                    JSONObject json = new JSONObject(res);
                    JSONArray jsnArry = json.getJSONArray("keys");
                    for (int i = 0 ; i < jsnArry.length() ; i++){
                         jsnObj = jsnArry.getJSONObject(i);
                    }
                    Log.d("jsnObj",jsnObj.toString());
                    Log.d("jsnArry",jsnArry.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.d("The Positive  are",res);

            }
Maqsood Alam
  • 27
  • 1
  • 7
0

You can try this (I have tested):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String jsonString = "{\n" +
                "  \"keys\" : [ \"10_Ultimate\", \"20_Yemensoft\" ]\n" +
                "}";

        JSONObject jsnObj  = new JSONObject();
        try {
            JSONObject json = new JSONObject(jsonString);
            JSONArray jsnArry = json.getJSONArray("keys");
            for (int i = 0 ; i < jsnArry.length() ; i++){
                jsnObj.put(String.valueOf(i), jsnArry.getString(i));
            }
            Log.d("jsnObj", jsnObj.toString());
            Log.d("jsnArry",jsnArry.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.d("The Positive  are", jsonString);
    }
BNK
  • 23,994
  • 8
  • 77
  • 87