【问题标题】:A JSONObject text must begin with '{' at 1 [character 2 line 1]JSONObject 文本必须在 1 [字符 2 第 1 行] 处以 '{' 开头
【发布时间】:2015-03-10 03:06:45
【问题描述】:

我正在尝试使用 javaapi 显示来自 gmail 的电子邮件。 当用户点击消息行时,邮件将打开其消息正文。为此,我编写了一个 javascript 函数:

function viewMail() {

    $('#table tbody').unbind().on(
            'click',
            'tr td:not(.email-select)',
            function() {

                var messageNumber = $(this).parent().children('td:eq(0)')
                        .children().val();
                var from = $(this).parent().children('td:eq(1)').text();
                var subject = $(this).parent().children('td:eq(2)').text();
                var dateAndTime = $(this).parent().children('td:eq(3)').text();
                var seen = $(this).parent().children('td:eq(4)').text();
                var folderName = $(this).parent().children('td:eq(5)').text();
                /*
                 * $.post("/Webclient/getMail", {messageId:messageId},
                 * function(data){
                 */
                var data = {"messageNumber":messageNumber,
                        "seen":seen,
                        "folderName":folderName
                        };

                $.ajax({
                    type : "GET",
                    url : "/Webclient/showMail",
                    contentType : "application/json; charset=utf-8",
                    data : JSON.stringify(data),
                    success : function(result) {
                        $('#email_subject').text(subject);
                        $('#sender_name').text(from);
                        $('#date_and_time').text(dateAndTime);
                        $('#messageBody').empty();
                        $('#messageBody').append(result.content);
                        $('#emailModal').modal('show');
                    }

                });

            });

}

这在我的控制器方法中:

@RequestMapping(value = "/showMail", method = RequestMethod.GET)
    public @ResponseBody
    String showMail(HttpServletRequest request) throws IOException{

        JSONObject result = new JSONObject();
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = request.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        String emailInfo = buffer.toString();

        //This is the line I am getting error 
        JSONObject jsonObject = new JSONObject(emailInfo);
        System.out.println(emailInfo);

        User authUser = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        String userName = authUser.getUsername();
        String password = SecurityContextHolder.getContext()
                .getAuthentication().getCredentials().toString();

        String message = null;

             try {
                message = imapService.showMail(userName,password,jsonObject);
                result.append("message",message);
            } catch (MessagingException e) {
                result.append("message","Could not open the message");
                e.printStackTrace();
            }

             return result.toString();

    }

错误:

org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    org.json.JSONTokener.syntaxError(JSONTokener.java:433)
    org.json.JSONObject.<init>(JSONObject.java:194)
    org.json.JSONObject.<init>(JSONObject.java:321)
    controller.ImapController.showMail(ImapController.java:173)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)

【问题讨论】:

    标签: javascript jquery json jakarta-ee


    【解决方案1】:

    错误消息“A JSONObject 文本必须以'{'开头”中的行让我怀疑问题出在JSONObject 构造函数中的emailinfo

    我不知道emailinfo 的内容来自这里,但我敢打赌,如果您查看打印输出的emailinfo 的内容,您会看到emailinfo不是正确的 JSON 格式。

    正确的 JSON 格式示例如下:

    {"key": {"second_key": "value"}}
    

    另外,我推荐以下站点来检查 emailinfo 是否是有效的 JSON:http://jsonlint.com

    【讨论】:

    • {"messageNumber":messageNumber, "seen":seen, "folderName":folderName }; JSON.stringify(数据) 。这是 emailInfo 中的 json 字符串
    • messageNumber(冒号后的第二次出现)需要在其周围加上引号 - 与 folderName 类似。试试我提到的链接,它很有帮助!
    • 此外,emailinfo 之前可能有空格或类似的东西,因此有关不以“{”开头的错误消息。我建议将 `emailinfo.trim()' 传递给您收到错误的行中的构造函数
    • 如果您觉得我的回答有用,请点赞/接受!将不胜感激
    【解决方案2】:

    我曾经收到过同样的错误信息。事实证明,问题在于 JSON 数据之前有一个空行,即“{”之前。

    关于 JSON 对象语法的类似问题。 希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      当您尝试对格式不正确的 json 进行任何操作时,您会遇到上述问题。
      首先尝试使用现有的 json 对象创建一个新的 Json 对象。如果您在现有 json 中没有任何错误,您将能够创建新的。然后对新的 json 对象进行操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多