为什么这个JSON字符串无效?(Why is this JSON String invalid? jQuery.parseJSON function throws invalid character)

编程入门 行业动态 更新时间:2024-10-27 14:34:26
为什么这个JSON字符串无效?(Why is this JSON String invalid? jQuery.parseJSON function throws invalid character)

当试图解析我的Serlvet返回给我的JSP页面的JSON时,下面的代码行会抛出一个无效的字符异常:

var obj = jQuery.parseJSON(data);

客户端代码

<script type = "text/javascript"> function updateProgrammerDetails(site, Id) { $.post('EngineAdminServlet', { action: "updateProgrammerMenu", siteDetails: site, ID: Id, }, function(data, status){ var pid, name, sky, ip, eleven; var obj = jQuery.parseJSON(data); ......... } }).fail(function(error) { alert("Error retrieving details from server"); }); //end ajax call } </script>

使用GSON生成的JSON字符串

我在我的Servlet类中导入了GSON库,以便将我的Java对象'ProgrammerForJSP'转换为JSON字符串。 在其他帖子中建议这样做,以避免在自己创建JSON字符串时出现任何拼写错误。 这是它返回的字符串:

{"ID":123,"siteName":"VEGAS","skyl":"WZ0019","ipAddress":"0.0.0.0","code":"L/BEG"}

SERVLET代码

.... response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); String gson = new Gson().toJson(myObject); response.getWriter().write(gson); } //end of method

我不知道我是否错过了一步或做错了什么? 我的想法是,IP地址字段可能会抛出解析器并破坏代码(因此无效的字符异常),因为它包含句点字符吗? 我真的不知道。

编辑我正在使用jquery-1.11.3.js

When trying to parse the JSON being returned by my Serlvet to my JSP page the following line of code is throwing a invalid character exception :

var obj = jQuery.parseJSON(data);

CLIENT SIDE CODE

<script type = "text/javascript"> function updateProgrammerDetails(site, Id) { $.post('EngineAdminServlet', { action: "updateProgrammerMenu", siteDetails: site, ID: Id, }, function(data, status){ var pid, name, sky, ip, eleven; var obj = jQuery.parseJSON(data); ......... } }).fail(function(error) { alert("Error retrieving details from server"); }); //end ajax call } </script>

JSON STRING GENERATED USING GSON

I've imported the GSON library in my Servlet class in order to convert my Java object 'ProgrammerForJSP' to a JSON string. This was recommended on other posts to avoid any typos when creating the JSON string myself. This is the string it returned:

{"ID":123,"siteName":"VEGAS","skyl":"WZ0019","ipAddress":"0.0.0.0","code":"L/BEG"}

SERVLET CODE

.... response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); String gson = new Gson().toJson(myObject); response.getWriter().write(gson); } //end of method

I don't know if I'm missing a step or doing something wrong? My thought is that the IP address field might be throwing off the parser and breaking the code (hence the invalid character exception) because it contains period characters in it? I really have no idea.

EDIT I'm using jquery-1.11.3.js

最满意答案

该错误是因为$.post方法已经检测到JSON响应并将结果解析为一个对象。 然后你试图在一个对象上调用parseJSON而不是JSON字符串,这会导致你所看到的错误。 您只需将呼叫移除到$.parseJSON 。 尝试这个:

function updateProgrammerDetails(site, Id) {
    $.post('EngineAdminServlet', {
        action: "updateProgrammerMenu",
        siteDetails: site,
        ID: Id,
    }, function(data, status) {
        var pid, name, sky, ip, eleven;
        // work with the object in 'data' here, E.g:
        console.log(data.ID, data.siteName); // = 123, 'VEGAS'
    }).fail(function(error) { 
        alert("Error retrieving details from server");
    }); //end ajax call
}

The error is because the $.post method has already detected a JSON response and parsed the result to an object for you. You are then trying to call parseJSON on an object instead of a JSON string, which results in the error you've seen. You simply need to remove the call to $.parseJSON. Try this:

function updateProgrammerDetails(site, Id) {
    $.post('EngineAdminServlet', {
        action: "updateProgrammerMenu",
        siteDetails: site,
        ID: Id,
    }, function(data, status) {
        var pid, name, sky, ip, eleven;
        // work with the object in 'data' here, E.g:
        console.log(data.ID, data.siteName); // = 123, 'VEGAS'
    }).fail(function(error) { 
        alert("Error retrieving details from server");
    }); //end ajax call
}

                    
                     
          

更多推荐

本文发布于:2023-08-04 06:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   invalid   String   JSON   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!