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 ();
}
-
can you post the whole JSON you recieved? and you can validate your JSON response using pro.jsonlint.orgSMR– SMR2014年06月23日 12:25:12 +00:00Commented Jun 23, 2014 at 12:25
-
your json is not validShayan Pourvatan– Shayan Pourvatan2014年06月23日 12:25:52 +00:00Commented Jun 23, 2014 at 12:25
-
that is not a valid JSON. it is supposed to throw exception.SMR– SMR2014年06月23日 12:27:04 +00:00Commented 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 authenticatedAnuj– Anuj2014年06月23日 12:29:07 +00:00Commented Jun 23, 2014 at 12:29
-
Use GSON instead of JSONParser.Hardik Joshi– Hardik Joshi2014年06月23日 12:29:16 +00:00Commented Jun 23, 2014 at 12:29
1 Answer 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
TmKVU
3,0002 gold badges19 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
default