【问题标题】:Help with JSON error handling and google translate api帮助处理 JSON 错误和谷歌翻译 api
【发布时间】:2011-04-13 02:50:00
【问题描述】:

我目前正在解析来自 Google Translate API 的 JSON 响应。我可以毫无问题地做到这一点。看到我没有太多的 XML 经验(我更像是一个 XML 人),我很难弄清楚如何在我的 JSON 解析中实现一些错误处理。我正在使用 JSON j2me 库。

这是一个成功的响应:

{"responseData": {"translatedText":"Teks te vertaal ...","detectedSourceLanguage":"en"}, "responseDetails": null, "responseStatus": 200}

这是一个不成功的响应:

{"responseData": null, "responseDetails": "could not reliably detect source language", "responseStatus": 400}

所以,如果翻译不成功,我想把“responseDetails”的值放到一个字符串中。这是我的解析代码,目前没有正确解析出 responseDetails。相反,“try”的“catch”被抓住了。

try {
            JSONObject responseObject = new JSONObject(response);
            if (responseObject != null) {
                JSONObject responseData = responseObject
                        .getJSONObject("responseData");
                if (responseData != null) {
                    String translatedText = responseData
                            .getString("translatedText");
                    Notify.alert(translatedText);
                } else {
                    String responseDetails = responseObject
                            .getString("responseDetails");
                    Notify.alert(responseDetails);
                }
            }
        } catch (Exception e) {
            Notify.alert("Unable to translate!");
        }

谁能看出我哪里出错了?

谢谢!

【问题讨论】:

    标签: java json parsing java-me


    【解决方案1】:

    既然您说触发了 catch 块,我将通过查看抛出的异常来开始调试。您可以简单地将警报字符串附加到包括 e.toString() 中。

    因此将 catch 块中的警报更改为:

    Notify.alert("Unable to translate! " + e.toString());
    

    看看抛出的实际错误是什么。

    根据您的评论,是的,它似乎正在尝试在 null 值上创建 JSONObject,因此请嵌套另一个 try/catch 块并以这种方式相应地对其进行解析。

    try {
        JSONObject responseObject = new JSONObject(response);
        if (responseObject != null) {
            /* Try create a new JSON object from the 
             * responseData object. If it fails, 
             * display an alert */
            try {
                JSONObject responseData = responseObject
                        .getJSONObject("responseData");
    
                if (responseData != null) {
                    String translatedText = responseData
                            .getString("translatedText");
                    Notify.alert(translatedText);
                } 
    
            } catch (Exception e) {
                String responseDetails = responseObject
                        .getString("responseDetails");
                Notify.alert(responseDetails);
            }
        }
    } catch (Exception e) {
        Notify.alert("Unable to translate outer block!");
    }
    

    【讨论】:

    • 感谢您的建议,creanium。以下是异常:org.json.me.JSONException: JSONObject["responseData"] 不是 JSONObject。我猜这是因为 responseData 为空?我只是想我仍然可以检查它是否为空,并采取相应的行动......
    • @behrk2 我已根据您的评论更新了我的原始答案。看看有没有帮助。
    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多