java - 使用 JQuery 解析 JSON 有困难

标签 java javascript jquery json dom

我开发了一个应用程序,将 Twitter 搜索结果作为 JSON 对象写入结果页面:

for (Status tweet : tweets) {
    Map<String, String> tweetResult = new LinkedHashMap<String, String>();
    tweetResult.put("username", tweet.getUser().getScreenName());
    tweetResult.put("status", tweet.getText());
    tweetResult.put("date", tweet.getCreatedAt().toString());
    tweetResult.put("retweets", String.valueOf(tweet.getRetweetCount()));
    String resultJson = new Gson().toJson(tweetResult);
    response.getWriter().write(resultJson);
}

在下面使用 AJAX/JQuery 调用:

$(document).ready(function() {                                                                      
    $.getJSON('SearchServlet', function(list) {
        var table = $('#resultsTable');
        $.each(list, function(index, tweet) {
        $('<tr>').appendTo(table)
                .append($('<td>').text(tweet.username))
                .append($('<td>').text(tweet.status))
                .append($('<td>').text(tweet.date))
                .append($('<td>').text(tweet.retweets));
        });
    });
});

为了用结果填充表格:

<body>
    <div id="wrapper">      
        <div id="contentArea">
            <div id="content">
                <h2>Results:</h2>
                <table id="resultsTable"></table>
            </div>
        </div>
    </div>
</body>

GET 调用运行良好,结果毫无问题地显示在 firebug 控制台中,但是它们并没有按预期出现在实际文档本身上。我为此尝试了多种不同的方法(包括答案​​ herehere)。

JSON 输出示例:

{"username":"Dineen_","status":"RT @TwitterAds: Learn how to put Twitter to work for your small business! Download our small biz guide now: https://t.co/gdnMMYLI","date":"Tue Feb 26 08:37:11 GMT 2013","retweets":"22"}

提前致谢。

最佳答案

看来你的序列化是错误的。由于您正在生成一系列串联的 JSON 对象,这些对象未正确包含在数组中。

当前无效的 JSON 响应:

{ ... } { ... } { ... } { ... }

而预期的 JSON 响应应该是:

[ { ... }, { ... }, { ... }, { ... } ]

无需手动执行此操作。如果你构建了正确的对象,Gson 可能会自动为你做这件事。例如,使用如下内容(未经测试):

List<Map<String, String>> tweetList = new LinkedList<Map<String, String>>();
for (Status tweet : tweets) {
    Map<String, String> tweetResult = new LinkedHashMap<String, String>();
    tweetResult.put("username", tweet.getUser().getScreenName());
    tweetResult.put("status", tweet.getText());
    tweetResult.put("date", tweet.getCreatedAt().toString());
    tweetResult.put("retweets", String.valueOf(tweet.getRetweetCount()));
    tweetList.add(tweetResult);
}
String resultJson = new Gson().toJson(tweetList);
response.getWriter().write(resultJson);

此修复后,您应该能够使用您的原始代码。


根据您的示例 JSON 输出,返回的输出是一个对象,而不是数组。您不需要在此处使用 $.each

$(document).ready(function () {
    $.getJSON('SearchServlet', function(tweet) {
        var table = $('#resultsTable');
        $('<tr>').appendTo(table)
                 .append($('<td>').text(tweet.username))
                 .append($('<td>').text(tweet.status))
                 .append($('<td>').text(tweet.date))
                 .append($('<td>').text(tweet.retweets));
    });
});

关于java - 使用 JQuery 解析 JSON 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096017/

相关文章:

java - 如何使用单独的类(JPA 注释)和 JAXB 注释并手动进行映射

java - ReportServer Eclipse 编辑和部署

javascript - 多用户 Facebook 共享器

javascript - jQuery .fadeOut() 然后 .show();不使用 keyup

javascript - Jquery DOM 事件在其后面的事件执行后调用

java - 是否可以设置文件的 POSIX 组?

java - 如何使用 toString 方法打印第三个对象中的两个对象?

javascript - 如何迭代无序 JSON 数据并排序到数组中

JavaScript - 用于删除对象中多个键的内置函数?

javascript - 使用 jQuery 访问 javascript 对象