我正在尝试从json文件中获取别名字符串,但是却遇到了例外情况,说别名不是字符串。 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();
    }

我正在获取所有其他String值,但是当我尝试获取别名String时,却遇到了异常,我不明白为什么?

这是我的json文件的示例。
{"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}

即使别名为NULL,它也应该获取空字符串,你们中的任何人都可以解释为什么它是这样发生的。

提前致谢

最佳答案

问题是什么

如果存在key的属性,但org.json.JSONObject#getString(String)null抛出JSONException。

为什么会发生

参见代码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();
}

去哪里
 public Object get(String key) throws JSONException {
     Object o = opt(key);
     if (o == null) {
         throw new JSONException("JSONObject[" + quote(key) +
                 "] not found.");
     }
     return o;
 }

它是通过这种方式实现的,它将在null上引发异常。 JSON规范允许使用null,所以这只是一个怪癖。参见http://www.json.org/

该怎么办

对于可选值,此包提供了opt(String)方法,例如optString(String)optString(String, String),请参见http://www.docjar.com/docs/api/org/json/JSONObject.html

我可能会用
... = questions.getOptString("alias"); //if you want `""` as null value

要么
... = questions.getOptString("alias", null); //if you want null

有效地记住,您应该使用getString和类似的API来获取所需的值,并使用getOptString和类似的API来获取可选的值。

10-07 19:28
查看更多