使用jquery将JSON数组从jsp传递到javascript [英] Passing JSON array from jsp to javascript using jquery

查看:94
本文介绍了使用jquery将JSON数组从jsp传递到javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了两天,并尝试了我认为可能的所有事情. 我使用GSON创建一个JSON数组.当我在通过jquery将其发送到Javascript之前打印出json字符串时,它看起来像这样:

I've searched for two days now and tried every possible thing I think. I use GSON to create a JSON array. When I print out my json string before sending it to Javascript via jquery it looks like this:

[{"var1":"hi","var2":"this","var3":"is"}] 

对我很好.我尝试通过以下2种方式发送给js:

Looks good to me. I've tried sending to js the following 2 ways:

String json = gson.toJson(googData, listType);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

String json = gson.toJson(googData, listType);
<%= json %>

这是我的js代码:

$.getJSON("testgoogle.jsp", function(json) {
    $.each (json, function(k, v) {
        alert(v.val1);
    });
});

很简单吧?我可以将其更改为$ .get并查看字符串.如果我提醒(k),它只会提醒0次.我怀疑我的json字符串传递了一些空格.当我警告从jsp返回的任何内容时,它在警告框中的显示要比说alert("hi")低.你知道在哪里!在警报框中,"hi"在中间的位置如何! img.好吧,当我从jsp发送<%="hi"%>时,它在!之下.我一直将<%="hi"%>一直移到顶部,所以我的jsp看起来像:

Pretty simple right? I can change it to $.get and see the string. If I alert(k) it only alerts 0 one time. I have a suspicion that i'm passing some white space with my json string. When I alert anything returned from the jsp's it appears lower in the alert box than say an alert("hi"). You know where the ! is in the alert box and how "hi" is right in the middle of the ! img. Well when I have <%= "hi" %> sent from jsp it is below the !. I moved <%= "hi" %> all the way to the top so my jsp looked like:

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%= "hi" %>

如您所见,我正在抓稻草.这在!的底部返回了"hi".我将jsp中的行越多,将"hi"移到警报框中的位置就越低.另外,有些奇怪.如果我将此保留在我的jsp顶部:

As you can see, I'm grasping at straws. This returned "hi" at the bottom of the !. The more lines in my jsp I move the "hi" down the lower it is in my alert box. Also, something weird. If I leave this at the top of my jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

这实际上会在我的警报中打印出来.

That will actually print out in my alert.

如果您需要更多信息,请告诉我.

Please let me know if you need more info.

解决方案: 当密钥为var1时,我试图调用val1.哇!我永远不会把所有这些小时都弄回来;)正确的代码与上面的相同,只是val1应该是var1.此外,我尝试使用eval(json)进行操作,但仍然可以参考.感谢您尝试帮助所有人!

Solution: I was trying to call val1 when the key was var1. Wow! I will never get all those hours back ;) The correct code is identical to the above except val1 should be var1. Also, I tried it with eval(json) and it still worked FYI. Thanks for trying to help everyone!

推荐答案

$ .getJSON()

成功回调传递给返回的数据,通常是 由JSON结构定义并解析的JavaScript对象或数组 使用$ .parseJSON()方法.它还传递了的文本状态 响应.

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.

您不必自己解析"字符串,因为jQuery已经对其进行了解析

You don't have to "parse" the string yourself as it is already parsed by jQuery

$.getJSON("testgoogle.jsp", function(json) {
    $.each (json, function(k, v) {
        alert(v.val1);
    });
});

这篇关于使用jquery将JSON数组从jsp传递到javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆