This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Looping through JSON String and Printing into table

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using javascript to loop through a function and I came across a solution on Stackoverflow that uses these nested for loops.  It isn't working for me though.  It just prints undefined to the table.



I'm including a picture of my JSON string.  I'm also
JSONstring.PNG
[Thumbnail for JSONstring.PNG]
JSON string in Chrome console
ersTable.PNG
[Thumbnail for ersTable.PNG]
Picture of the table
 
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, for example, "each.id" in line 13 is undefined. Using your JavaScript debugger should show you why that is in no time at all. It looks like you're making some incorrect assumptions about what's in that "table_values" variable or maybe whatever calls that function isn't passing the right thing, but like I say the debugger should help you track down that sort of thing.
 
Paul Clapham
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it looks like you want to create more than one row in your table, based on what I see in your JSON data. But your code only creates one.

Fortunately you just asked the same question about your Java code creating only one object instead of a list of different objects. The answer to that question is the answer to this question too.
 
Andrew Spiteri
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue I'm having understanding your help is that when my code looked like this:



And there was only one record in the database, it worked fine.  I could parse the JSON string and it wasn't an issue.  Now I can't figure how to loop through the data.  I have multiple records in my database so when I say table_values.id it can't decipher between which id I'm referring to.

I found this answer in StackOverflow: https://stackoverflow.com/questions/3757495/javascript-looping-through-a-json-string

His JSON looks like this at the beginning:

I'm not sure how'd you make Java do that, though.  This is what my servlet code looks like:



 
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:I'm including a picture of my JSON string.


Can you include the actual text rather than an image?  Your problem may be related to the data rather than the code.
 
Ron McLeod
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:


I'm not sure why there are two for loops.  I would probably use something like:
 
Andrew Spiteri
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what the JSON string looks like:



The issue still occurs when I only use one for loop:



Now it prints a row for what seems like each character in the JSON string.

I'm including a screen shot of the output.

tableScreenShot.PNG
[Thumbnail for tableScreenShot.PNG]
Screenshot of table output
 
Ron McLeod
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:Here's what the JSON string looks like ...



Your JSON data is not valid.  It currently looks like this (line breaks and indentation added for readability):
But should look like this - an array of object with each object separated with a comma:
 
Ron McLeod
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:His JSON looks like this at the beginning: I'm not sure how'd you make Java do that, though.  This is what my servlet code looks like ...


This isn't exactly the same, but here's an example of how you could use the mapper to create the structure that looks like this:
Structure the data with Java object to represent the structure of the JSON data, and then pass the mapper the the top-level element to serialize:
Data classes I used for example:
 
Ron McLeod
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrew Spiteri wrote:The issue still occurs when I only use one for loop:


If you fix your JSON data formatting, the undefined problem should go away.

You need to provide a string value for innerText so status.innerText = item.status should be something like status.innerText = String(item.status).

Also toDateString is a function, not a property, so it should be toDateString().
 
Ron McLeod
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, since the value of completed provided by the server could be null, you should guard against interpreting that value as representing time otherwise you may find that your table show something January 1, 1970, which is obviously wrong.

Maybe something like this:
 
Andrew Spiteri
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.  I really need to learn more about ObjectMapper and how it works to get a grasp of what I just did.

My trainer sent me some stuff from Baeldung on ObjectMapper I have yet to read, from before we dove into servlets.  I guess I'll be doing that when I get some time.

Thanks again.
 
Ron McLeod
Saloon Keeper
Posts: 2955
378
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe focus on the front-end first and get the data representation and rendering figured-out first, and then move on to the back-end.

For example - start with putting the data inside your table rendering function and get that to work, then provide the the data from another function or a command-line tool (such as netcat) which could mock what the backend would send, then move on to actually getting from the back-end.
This is what I see on my browser (I didn't apply any styling):

 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!