繁体   English   中英

org.json.JSONException: JSONObject[“alias”] 不是字符串

[英]org.json.JSONException: JSONObject[“alias”] not a string

我正在尝试从 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 它也应该得到空字符串你们中的任何人都可以解释为什么会这样。

提前致谢

问题是什么

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

为什么会发生

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

get 在哪里

 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 s,所以这只是一个怪癖。 http://www.json.org/

怎么办

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

我可能会用

... = questions.optString("alias"); //if you want `""` as null value

或者

... = questions.optString("alias", null); //if you want null

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

您应该像这样检查它是否为空:

String alias = "";
if(!json.isNull("alias")){
    alias = json.getString("alias");
} else {
    alias = null;
}

如果您不想这样做,那么您可能需要使用不同的 json 库。

它的发生是因为“别名”:空。 这并不意味着 "alias":"null" 或 "alias":"" 那样。 这是一个不同的东西,不是一个字符串值。 先删除这一行

 System.out.println("HELLO    "+questions.getString("alias"));

并获取“别名”键和其他可能具有空值的键的值...

if(!questions.isNull("alias")){ //Returns true if this object has no mapping for name or if it has a mapping whose value is NULL.
String abc = questions.getString("alias");
}

希望能帮到你...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM