0

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 ();
 }
Tejas Kale
4158 silver badges18 bronze badges
asked Jun 23, 2014 at 12:24
6
  • can you post the whole JSON you recieved? and you can validate your JSON response using pro.jsonlint.org Commented Jun 23, 2014 at 12:25
  • your json is not valid Commented Jun 23, 2014 at 12:25
  • that is not a valid JSON. it is supposed to throw exception. Commented Jun 23, 2014 at 12:27
  • means i will get an error?? or else how to parse it cos when NewDataSet = blanks this means user is not authenticated Commented Jun 23, 2014 at 12:29
  • Use GSON instead of JSONParser. Commented Jun 23, 2014 at 12:29

1 Answer 1

1

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");
answered Jun 23, 2014 at 12:29
Sign up to request clarification or add additional context in comments.

2 Comments

{"success":"0"} I have this json nw how do i parse it to get the value of success
@user3667909 I have edited my answer. Note that you can also put integers in json objects: {"success":0} and retrieve it with jsonObject.getInt("success");

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.