0

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"}] 

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);

and

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

Here's my js code:

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

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" %>

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.

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!

8
  • as a sidenote, why do you do this from inside a JSP? The former code is identical to what you'd use in a servlet, which would a) be a lot cleaner and b) you wouldn't have to worry about things like white space generated by JSP directives & co. Commented Jun 29, 2012 at 21:41
  • I guess I just never thought to try. Are you talking about not using JQuery? Commented Jun 29, 2012 at 21:49
  • not at all, just suggesting you replace your server side JSON generating JSP by a plain servlet. JSPs are a view technology, there's not a lot to view here :-) Commented Jun 29, 2012 at 21:51
  • ouch, that sounds very painful - not familiar with WAS7, doesn't it support redeployment of wars? Commented Jun 29, 2012 at 22:05
  • Can i ask what the js would look like? I've mostly done Java programs and other scripting. What would the callback look like? $.getJSON("url_to_servlet".....? Commented Jun 29, 2012 at 22:12

3 Answers 3

1

$.getJSON()

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.

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);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

i'm not sure what you changed here? I tried this: $.getJSON("testgoogle.jsp", function(json) { alert(json.val1) It's undefined
I've removed the 'eval(...)' as you've done it yourself. 'json' is an array so you'll have to use 'json[0].val1' if you're not going to use $.each anymore
0

In $.getJSON the json in function(json) { is alraedy a JavaScript object so you don't eval it

1 Comment

Yea. I tried that. I've tried 100's of variations of the js syntax it seems. I even tried $.makeArray and $.map.
0

Try this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.