0

I have a Spring @Controller that is taking an existing ArrayList and creating a JSON envelope. When I display the JSON Envelope in the Spring Console, I see the data that I want is in there. When I view the same JSON Envelope in FireBug in Firefox, I also see the data that I want.

I'm attempting to display the envelop on the jsp page using JavaScript, and I really have no idea how to even start.

In other modules of the code, there are areas where it's getting information from a DataBase, using YUI2 to display it with a DataTable. It also uses a JSON Envelope, and it works. I'm trying to copy this to display just the JSON text string on the JSP page. I don't know if I need to use YUI2, or if I can just use JavaScript to access the JSON Envelope.

I am brand new to Java and JSON and the Spring environment, so I don't even have any idea what sort of JavaScript syntax to use to access the JSON Envelope, much less display it. I've been Googling to find an answer, but so far most of it is above my head.

I'd appreciate any help. Thanks.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Cynthia
  • 151
  • 1
  • 13
  • The problem seems to be that you don't know how to consume a JSON string in JavaScript (jQuery or other JS library) side. It will be good to start with the basics. Some sites to learn: [JSON in JavaScript](http://www.json.org/js.html), [How to parse JSON in JavaScript](http://stackoverflow.com/q/4935632/1065197) and [JSON Tutorial](http://www.w3schools.com/json/) – Luiggi Mendoza Apr 09 '13 at 17:22
  • By the way, it would be better to learn the basics before getting mixed with a set of libraries that ease the work like YUI2 (note that this advice would apply on almost every set of technologies, not just for JSON). – Luiggi Mendoza Apr 09 '13 at 17:26
  • Thanks for the links I'll go read up. :) This project was built by someone else, and I've been pushed in with no training and trying to learn. The one who built it used YUI2, and I've never used that either, so I'm learning that as well. – Cynthia Apr 09 '13 at 20:14

2 Answers2

0

Your Question is very unclear.

If you simply want to display the text of the JSON, then you don't need to use javascript at all. Just use <c:out> to output the JSON string into the relevant part of a plain HTML page.

You would use Javascript if you wanted to generate dynamic HTML on the client side. And at that point you'd probably want the Javascript to use an XMLHttpRequest object to call to the server to request the JSON. The Javascript would then be responsible for dynamically turning that JSON response into stuff that can be displayed ... and you'd definitely be wanting a client-side JS framework to help with that.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for your reply. I tried the `` just in the html, to see if it'd work, and Spring gives me an error... `Multiple annotations found at this line: - Missing required attribute "value" - No end tag ().` I would love to be more clear, but half the time I don't really know what question I need to ask... – Cynthia Apr 09 '13 at 20:27
  • @Cynthia - You **really, really** need to do some basic tutorials on the technology you are trying to use. In this case JSPs and JSTL. For example ... http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html. If you take the time to read the tutorial, you will at least know what questions to ask ... and what that error message means. (It means that you have used the wrong syntax. Tags need either a closing tag, or they need to be self-closing. And you haven't told the JSP *what* you want to output using the tag.) – Stephen C Apr 09 '13 at 23:46
0

To parse the retrieved JSON you can use $each method of jQuery

var obj = {
 "Name": "Robert",
 "Counrty": "USA",
 "Address" : {"Apartment":"111","City":"Atlanta","State":"GA","Zip":"30005" }
 };

$.each( obj, function( key, value ) {
 alert( key + ": " + value );

if(typeof(value)=="object")
{
    $.each( value, function( key, value ) {
          alert( key + ": " + value );
        });
 }
});

If you have more complex JSON then you have to repet $each for each complex type as I did for Address.

Then you can use the Key value to fetch the data from the JSON object.

Working example

NullPointerException
  • 3,732
  • 5
  • 28
  • 62