Anuj
Anuj

Reputation: 129

Error in Json Android

When i call an webservice i receive response in json format.The app works fine but when I receive json like { "NewDataSet": }} my app runs into catch statement and I m getting error like this - org.json.JSONException: Expected literal value at character 16 of { "NewDataSet": }}

Code to parse json

 protected void onPostExecute(String s) {
            super.onPostExecute (s);

            try {
//                jsonObject = new JSONObject (s);
                String response=s.trim ();
                jsonObject = new JSONObject (response);
                if (NewDataSet == null) {
                    Toast.makeText (Login.this, "error", Toast.LENGTH_SHORT).show ();
                }
                NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                Table = NewDataSet.getJSONObject ("Table");
                String User_ID = Table.getString ("User_ID");
                String Vendor_IEntity_Code = Table.getString ("Vendor_IEntity_Code");
                String Vendor_Name = Table.getString ("Vendor_Name");

                SettingPreference.setUserId (Login.this, User_ID);
                SettingPreference.setVendorId (Login.this, Vendor_IEntity_Code);
                SettingPreference.setVendorName (Login.this, Vendor_Name);

                Log.e ("Json response", "" + User_ID + "" + Vendor_IEntity_Code + "" + Vendor_Name);

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

Upvotes: 0

Views: 103

Answers (1)

TmKVU
TmKVU

Reputation: 3000

Your JSON is simply not valid. You need to assign a value to 'NewDataSet'. If you want this to be empty you can set the value to an empty String like ""

{ "NewDataSet": ""}

to retrieve this data you can use:

String result = jsonObject.getString("NewDataSet");

Upvotes: 1

Related Questions