By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Iterate through JSON object that is returned in response

P: 1
I have the following question:
Im doing a sample application using dojo and json.

I have 2 classes:
1. Book class
package com.esolaria.dojoex;

Expand|Select|Wrap|Line Numbers
  1. import org.json.JSONObject;
  2. import org.json.JSONException;
  3.  
  4. public class Book {
  5.  
  6.     private int bookId;
  7.     private String title;
  8.     private String isbn;
  9.     private String author;
  10.  
  11.  
  12.     public Book(int bookId, String title, String isbn, String author) {
  13.         this.bookId = bookId;
  14.         this.title = title;
  15.         this.isbn = isbn;
  16.         this.author = author;
  17.     }
  18.  
  19.  
  20.     public void setBookId(int bookId) {
  21.         this.bookId = bookId;
  22.     }
  23.     public int getBookId() {
  24.         return this.bookId;
  25.     }
  26.  
  27.     public void setTitle(String title) {
  28.         this.title = title;
  29.     }
  30.     public String getTitle() {
  31.         return this.title;
  32.     }
  33.  
  34.     public void setIsbn(String isbn) {
  35.         this.isbn = isbn;
  36.     }
  37.     public String getIsbn() {
  38.         return this.isbn;
  39.     }
  40.  
  41.     public void setAuthor(String author) {
  42.         this.author = author;
  43.     }
  44.     public String getAuthor() {
  45.         return this.author;
  46.     }
  47.  
  48.     public String toJSONString() throws JSONException {
  49.         JSONObject jsonObj = new JSONObject();
  50.         jsonObj.put("bookId", new Integer(this.bookId));
  51.         jsonObj.put("title", this.title);
  52.         jsonObj.put("isbn", this.isbn);
  53.         jsonObj.put("author", this.author);
  54.         return jsonObj.toString();
  55.     }
  56.  
  57. }
2. BookManager class.

Expand|Select|Wrap|Line Numbers
  1. package com.esolaria.dojoex;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import org.json.JSONObject;
  7. import org.json.JSONException;
  8.  
  9. public class BookManager {
  10.  
  11.     private static List books = new ArrayList();
  12.     static {
  13.         books.add(new Book(1, "Crime and Punishment", "0679734503", "Fyodor Dostoevsky"));
  14.         books.add(new Book(2, "The Collected Tales of Nikolai Gogol", "0375706151", "Nikolai Gogol"));
  15.         books.add(new Book(3, "King Rat", "0440145465", "James Clavell"));
  16.         books.add(new Book(4, "The Alchemist", "0062502182", "Paulo Coelho"));
  17.         books.add(new Book(5, "A Tale of Two Cities", "0451526562", "Charles Dickens"));
  18.     }
  19.  
  20.  
  21.     public static Book getBook(int bookId) {
  22.         Book returnValue = null;
  23.         for (Iterator iter = books.iterator(); iter.hasNext();) {
  24.             Book book = (Book) iter.next();
  25.             if (book.getBookId() == bookId){
  26.                 returnValue = book;
  27.                 break;
  28.             }
  29.         }
  30.         return returnValue;
  31.     }
  32.  
  33.     public static List getAllBooks() {
  34.         books.size();
  35.         List book = BookManager.getBooks();
  36.         for(Iterator item = book.iterator(); item.hasNext();){
  37.             Book books = (Book) item.next();
  38.             System.out.println(books.getBookId());
  39.             System.out.println(books.getAuthor());
  40.             System.out.println(books.getIsbn());
  41.             System.out.println(books.getTitle());
  42.         }
  43.         return book;
  44.     }
  45.  
  46.     public static List getBooks() {
  47.         return books;
  48.     }
  49.  
  50. }

The middle layer is jsp: getAllBooks.jsp

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.util.Iterator,
  2.          java.util.List,
  3.          com.esolaria.dojoex.Book,
  4.          com.esolaria.dojoex.BookManager" %>
  5. <%
  6.         List book = BookManager.getBooks();
  7.         for(Iterator item = book.iterator(); item.hasNext();){
  8.             Book books = (Book) item.next();
  9.             try{
  10.                 out.println(books.toJSONString());
  11.  
  12.             }catch(Exception e){
  13.                 System.out.println(e);
  14.             }
  15.         }//for loop ends
  16.  
  17. %>

My index file is having the following code:

[HTML]<%@ page
import="java.util.Iterator,
java.util.List,
com.esolaria.dojoex.Book,
com.esolaria.dojoex.BookManager" %>
<% List books = BookManager.getBooks();%>
<html>
<body>
<head>
<title>Example 1</title>
<script language="javascript" src="../js/dojo/dojo.js"></script>
<script language="javascript">
dojo.require("dojo.io.*");
dojo.require("dojo.event.*");
dojo.require("dojo.html.*");

function trMouseOver(bookId) {
getBookInfo(bookId);
}

function trMouseOut(evt) {
//var bookDiv = document.getElementById("bookInfo");
//bookDiv.style.display = "none";
}

function getBookInfo(bookId) {
//var params = new Array();
//params['bookId'] = bookId;
var bindArgs = {
url: "../actions/getAllBooks.jsp",
error: function(type, data, evt){alert("error");},
mimetype: "text/plain"
//content: params
};
var req = dojo.io.bind(bindArgs);
dojo.event.connect(req, "load", this, "populateDiv");
}

function populateDiv(type, data, evt) {

var result = "";
for(var i = 0; i < 5; i++){
var result += data.bookId +","+ data.title +","+ data.isbn +","+ data.author;
}
alert(result);


}



</script>
</head>
<body>
<h1>Books</h1>
<p>
Hover over book title for more information.
</p>
<table border="1" cellspacing="1" cellpadding="3" style="background-color:lavender; border: solid 1px #CCCCCC">
<% for (Iterator iter = books.iterator(); iter.hasNext();) {
Book book = (Book) iter.next(); %>
<tr onmouseover="trMouseOver(<%=book.getBookId()%>)" onmouseout="trMouseOut(<%=book.getBookId()%>)">
<td><%=book.getTitle()%></td><td></td>
</tr>
<% } %>
</table>
<div id="bookInfo" style="display:none;"></div>
</body>
</html>
[/HTML]

QUESTION:

Im getting the response as a list but wanted to know how to iterate through that list to get the information of each individual book in seperate row of the table.

Thanks in advance.
Dec 22 '07 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,320
hi ...

for that purpose you have to create the respective tr, td and textnodes and append it to the table ... you should use the:

- createElement();
- createTextNode();
- appendChild();

dom-methods for this. have a look here ... there you find more information about dom-interfaces ...

kind regards
Dec 26 '07 #2

RMWChaos
100+
P: 137
Wait a sec...is this the right forum to post this question? I was wondering why you were using "private" and "public" -- that's JAVA not JavaScript. Looks like you got your answer, but I wonder if this post shouldn't be moved to the Languages - JAVA forum instead.
Dec 26 '07 #3

gits
Expert Mod 5K+
P: 5,320
Wait a sec...is this the right forum to post this question? I was wondering why you were using "private" and "public" -- that's JAVA not JavaScript. Looks like you got your answer, but I wonder if this post shouldn't be moved to the Languages - JAVA forum instead.
it IS a javascript question ... have a look at the last part of the posted code :)

kind regards
Dec 26 '07 #4

RMWChaos
100+
P: 137
it IS a javascript question ... have a look at the last part of the posted code :)

kind regards
LOL, I stopped reading after I saw the JAVA code. I gotta stop jumping to conclusions. :-)
Dec 26 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.