I'm trying to get the alias string from the json file.But I'm getting the exception saying alias is not a string. org.json.JSONException: JSONObject["alias"] not a string.
String qsCurrentLine;
try {
brq = new BufferedReader(new FileReader("/home/storm/Videos/2.json"));
try {
while ((qsCurrentLine = brq.readLine()) != null) {
//System.out.println(qsCurrentLine);
//reads the line makes it json object
JSONObject question=new JSONObject(qsCurrentLine);
//System.out.println(question);
JSONArray usersarray=question.getJSONArray("users");
//System.out.println(usersarray);
for(int i=0;i<usersarray.length();i++){
JSONObject questions=usersarray.getJSONObject(i);
//System.out.println(questions);
int qus=questions.getInt("id");
//System.out.println(qus);
//String alias=questions.getString("alias");
//String check=questions.getString("answer");
//System.out.println(check);
String check1=questions.getString("question");
//System.out.println(check1);
String check2=questions.getString("_subtype");
//System.out.println(check2);
String check3=questions.getString("_type");
//System.out.println(check3);
String check4=questions.getString("options");
//System.out.println(check4);
System.out.println("HELLO "+questions.getString("alias"));
// System.out.println(check5);
int check6=questions.getInt("id");
System.out.println(check6);
//System.out.println("This alias " +aliass);
}
//JSONObject data25=array.getJSONObject(25);
//System.out.println(data25);
//question1060=data25.getString("question_36__option_10160_");
//System.out.println("the question number of 1060: "+question1060);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm getting all other String values but when I try to get the alias String I'm getting the exception I don't understand why?
Here this the sample of my json file.
{"answer":null,"question":"<h1 style=font-family:Lato, sans-serif;font-weight:600;font-size:28px;> Your response to our survey will help us identify how we are serving the needs of different companies.<\/span><\/span>","_subtype":"instructions","_type":"SurveyDecorative","options":"[]","alias":null,"id":73}
even if alias is NULL it should get the null string can any of you explain this why it is happening like this.
Thanks in Advance
org.json.JSONObject#getString(String)
throws JSONException if property for key exists, but is null
.
See code http://grepcode.com/file/repo1.maven.org/maven2/org.json/json/20080701/org/json/JSONObject.java
public String getString(String key) throws JSONException {
return get(key).toString();
}
where get is
public Object get(String key) throws JSONException {
Object o = opt(key);
if (o == null) {
throw new JSONException("JSONObject[" + quote(key) +
"] not found.");
}
return o;
}
It's just implemented this way, that it will throw exception on null
. JSON specification allows null
s, so it's just a quirk. See http://www.json.org/
For optional values this package provides method opt(String)
and e.g. optString(String)
or optString(String, String)
, see http://www.docjar.com/docs/api/org/json/JSONObject.html
I would probably use
... = questions.optString("alias"); //if you want `""` as null value
or
... = questions.optString("alias", null); //if you want null
Effectively keep in mind that you're expected to use getString
and similar APIs to get required values and optString
and similar APIs to get optional values.
You should check if it is null like this:
String alias = "";
if(!json.isNull("alias")){
alias = json.getString("alias");
} else {
alias = null;
}
If you don't want to do this, then you might want to use a different json library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With