• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble consuming JSON format in JSP  RSS feed

 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I am stuck with this code. I am new to JSON. I am trying to write MySQL data to a json file. Problem is the format of the file. The format of the file is

{"ColumnData":["Customer_ID","Title","First_Name","Middle_Initial"],"Rowdata":["10","Ms.","Flavia","Acevedo","11","Mr.","Piper","Santos"]}

but I want it to be something like

[{"Customer_ID": "10","Title":"Ms.","First_Name":"Flavia","Middle_Initial":"Acevedo"},
{"Customer_ID": "11","Title":"Mr.","First_Name":"Piper","Middle_Initial":"Santos"}]

My JSONWrite class is:





I have search everywhere on web but still unable to understand. Please help me out. Thank you in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to change the format? The existing format is "more correct" and also allows for extending in the future. For example, what if you eventually need to add metadata to the construct?
 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your reply, if it it good than i do not know how to read that format and display it in asp table
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I would say that learning how to use ASP more effectively is the right approach rather than changing the response structure to something less versatile.

I'm not sure if there are many ASP experts here who can help you as these forums are not focused on Microsoft technologies.

But someone may chime in -- I've changed your subject to better attract the ASP-savvy.

 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Then I would say that learning how to use ASP more effectively is the right approach rather than changing the response structure to something less versatile.

I'm not sure there are many ASP experts here who can help you as these forums are not focused on Microsoft technologies.

But someone may chime in -- I've changed your subject to better attract the ASP-savvy.


I am really sorry i was typo error (auto correct feature). its JSP nor ASP
 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again thank you so much for your reply. At least now i am satisfied with the format of my output file. need to explore more about reading json files
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that's different. I'll move this to the JSP forum and change the title again.

You'll want to show how the JSON is being used in the JSP.
 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Oh, that's different. I'll move this to the JSP forum and change the title again.

You'll want to show how the JSON is being used in the JSP.

Actually now i want to read this json file and display its contents in a JSP table in such a manner that Column data become the header and row data become the rows single record per row

Customer_ID Title First_Name
10 Mr Jonny
11 Ms Alec
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the best thing to do would be to NOT do it in a JSP, but to do it in a servlet, or some helper class. Write some Java code to generate a suitable data structure, then put that data structure into request scope and forward to a JSP. All that JSP needs to do is to transform the data structure to HTML, which should be simple.
 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Then the best thing to do would be to NOT do it in a JSP, but to do it in a servlet, or some helper class. Write some Java code to generate a suitable data structure, then put that data structure into request scope and forward to a JSP. All that JSP needs to do is to transform the data structure to HTML, which should be simple.


Alright, I tried earlier it in a java class that will be called from JS


am i doing it the right way?
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you're doing it the right way. You want the two records in your sample JSON code to be displayed in two separate rows of a table, so your Java code should be generating a data structure with two elements in it, each of which contains a row from the database in some way.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would this be done in Java code?

Why not just make an Ajax call to get the response and deal with it in JavaScript like everyone else on the face of the the Earth?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why would you want to change the format? The existing format is "more correct" and also allows for extending in the future. For example, what if you eventually need to add metadata to the construct?


More correct? I would have thought the second format would have been more better in terms of being more "object oriented" and more in keeping with the JSON data structures I have seen to date.
Yes it would be more verbose, in that the field names would be repeated, but it has the advantage of being convertible directly in to javascript objects.

Effectively the data structure for a row here is in java terms an ArrayList<Object> with list.get(0) being the id, list.get(2) being the title etc etc.
It is normally prefereable to have a java object with properties - which is in keeping with the second example.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "incorrect" (note quotes) format suffers from having an array at top level. Bad idea for reasons I pointed out.

I wasn't particularly commenting on the internal structure from there.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh I see. Yes, I also agree that having a top level array element for JSON is a bad idea.
But there was also a fundamental change in the structure of the data, and I think the OP may have been put off doing that change after your comment that the first one was "more correct".
So could we agree that a structure like:



has the best of both worlds? i.e. It has a top level JSON Object, but also maps properties to values directly.
We can then proceed to a discussion about property naming conventions such as using camel case rather than underscores and how to actually achieve this.

To produce this output, it is basically mapping the column name to the value in that column.


This reads from the database, and generates a JSON structure based on the column names.

It may be better to define a POJO that does the translation from the database, and return a List of Beans from your data access layer.
You can then take that list of beans and convert into JSON format for sending to the client.
One more layer of abstraction, but if you are going to be using the bean in the java layer, then you're going to need to do it anyway, and it decouples your JSON from your database column names.


 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't think you're doing it the right way. You want the two records in your sample JSON code to be displayed in two separate rows of a table, so your Java code should be generating a data structure with two elements in it, each of which contains a row from the database in some way.


Thank you so much for your comment. The above code worked very well for me and by formatting the data i can get read and display the contents of the json file.

Thank you so much all of you for guiding and helping me.
 
Assh Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why would this be done in Java code?

Why not just make an Ajax call to get the response and deal with it in JavaScript like everyone else on the face of the the Earth?


yes you are right and i agree with you but i have to develop it in java. Thank you so much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!