-3

I am getting an array of JSON from server. But I am trying to access it I am getting the Unterminated object exception.

I checked the output for any comma missed or wrong syntax but still not working.

JSON array

    [
  {
    "cust_id": "8",
    "mer_id": "3",
    "fav": "1",
    "cont_name": "sandeep",
    "kirana_name": "kasar",
    "phone_no": "9422879610",
    "email_id": "kasar1@gmail.com",
    "address": "nashik"
  },
  {
    "cust_id": "8",
    "mer_id": "1",
    "fav": "1",
    "cont_name": "sandeep",
    "kirana_name": "general store",
    "phone_no": "1212345623",
    "email_id": "kasar@gmail.com",
    "address": "nashik road nashik"
  },
  {
    "cust_id": "8",
    "mer_id": "5",
    "fav": "0",
    "cont_name": "a",
    "kirana_name": "s",
    "phone_no": "9422879610",
    "email_id": "sam@gmail.com",
    "address": "aa"
  },
  {
    "cust_id": "8",
    "mer_id": "6",
    "fav": "0",
    "cont_name": "see ya",
    "kirana_name": "see ya kirana",
    "phone_no": "8698766460",
    "email_id": "deepmalasingh55@gmail.com",
    "address": "dee"
  },
  {
    "cust_id": "8",
    "mer_id": "5",
    "fav": "0",
    "cont_name": "a",
    "kirana_name": "s",
    "phone_no": "9422879610",
    "email_id": "sam@gmail.com",
    "address": "aa"
  },
  {
    "cust_id": "8",
    "mer_id": "9",
    "fav": "0",
    "cont_name": "fre",
    "kirana_name": "fee kirana",
    "phone_no": "8698766460",
    "email_id": "deepmalasinghop@gmail.com",
    "address": "plotno:12,sahaydri nagar"
  }
]

Retrieving data from array :

 @Override
    protected void onPostExecute(JSONObject response) {
        super.onPostExecute(response);
        progressDialog.dismiss();
        try {
            merchantsList.clear();
            JSONArray jsonArray = response.getJSONArray("array");
            if(jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (jsonObject.has("message")) {
                        String message = jsonObject.getString("message");
                        Snackbar snackbar = Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG);
                        snackbar.show();
                    } else {
                        long id,merchant_id,item_quantity;
                        String kirana_name,created_at,address;
                        int fav;

                         merchant_id=jsonObject.getLong("mer_id");
                        kirana_name=jsonObject.getString("kirana_name");

                        fav=jsonObject.getInt("fav");
                        address=jsonObject.getString("address");


                        Merchants merchants = new Merchants(merchant_id,kirana_name,address,fav);
                        merchantsList.add(merchants);
                        merchantsAdapter.notifyDataSetChanged();

                    }
                }

            }

exception :

    org.json.JSONException: Unterminated object at character 973 of {"array":[{"cust_id":"8","mer_id":"3","fav":"1","cont_name":"sandeep","kirana_name":"kasar","phone_no":"9422879610","email_id":"kasar1@gmail.com","address":"nashik"},{"cust_id":"8","mer_id":"1","fav":"1","cont_name":"sandeep","kirana_name":"general store","phone_no":"1212345623","email_id":"kasar@gmail.com","address":"nashik road nashik"},{"cust_id":"8","mer_id":"5","fav":"0","cont_name":"a","kirana_name":"s","phone_no":"9422879610","email_id":"sam@gmail.com","address":"aa"},{"cust_id":"8","mer_id":"6","fav":"0","cont_name":"see ya","kirana_name":"see ya kirana","phone_no":"8698766460","email_id":"deepmalasingh55@gmail.com","address":"dee"},{"cust_id":"8","mer_id":"5","fav":"0","cont_name":"a","kirana_name":"s","phone_no":"9422879610","email_id":"sam@gmail.com","address":"aa"},{"cust_id":"8","mer_id":"9","fav":"0","cont_name":"fre","kirana_name":"fee kirana","phone_no":"8698766460","email_id":"deepmalasinghop@gmail.com","address":"plotno:12,sahaydri nagar"}]895da9a9a4cdd0825848479ee7e6c7ed2}
11-28 10:23:52.302 27004-27069/com.kiranaapp W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)

What's going wrong?? Any help will be great..Than you..

Sid
  • 2,792
  • 9
  • 55
  • 111
  • 1
    *What's going wrong?* Your json is wrong ... And no, it is not the same json as you wrote in the question – Selvin Nov 28 '16 at 10:29
  • @Sid The Json you posted in code is completely different from the json in the Logcat so the [json is invalid](http://i.imgur.com/tHJ6Gg8.png) – Maveňツ Nov 28 '16 at 10:32

2 Answers2

1

Yes this is because your Json array contains invalid string at the end. this is the string. 895da9a9a4cdd0825848479ee7e6c7ed2 Remove this and try again.

EDIT

You can check it by yourself. Go to this site Json Parser and then paste your Json array there. Then you can notice an error.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
0

There is no such element like "array" in your JSON, which you are using it. JSON should have some key for each value.

JSONArray jsonArray = response.getJSONArray("array");

Here in this case you array of JSON should have "array"=[]

Jimit Patel
  • 4,265
  • 2
  • 34
  • 58