aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes How to confirm the JSON return correctly and handle it from given format. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to confirm the JSON return correctly and handle it from given format." Watch "How to confirm the JSON return correctly and handle it from given format." New topic
Author

How to confirm the JSON return correctly and handle it from given format.

Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Good day,

Would like to get some guidance from guru here on how the JSON format look like and how could we used jQuery to retrieve the result.

The format show from FF10.2 from FireBug, is this the right format to return from respond server(using struts2-JSON plugin)?


if it is, how could we display record like above-mentioned?

The code that try from my end


doRenderJSONResult function:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Nakataa Kokuyo wrote:
The format show from FF10.2 from FireBug, is this the right format to return from respond server(using struts2-JSON plugin)?


Are the backslashes really in the response? They should not be. Backslashes are used in string literals to escape the quote characters but they are not part of JSON.

The actual response should be:

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Hi Bear,

That's strange as FF 10 is showing me output that post, while Chrome showing whatever you show, so i guess the JSON format which was return are correct.

My question is how to get parse and display the output ?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

If properly formed JSON is returned, the parsed data will be passed as the first argument to the success callback.
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Good day Bear,

Here is really what I'm seeing from Chome console, I confuse on the correctness

Under response tab:


Under JSON tab:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Well, are you getting the correct structure passed to the success callback?
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Hi Bear,

I'm using struts2 and passing a big List to GSON plugin to generate out the a String, I modify little bit on the output and doesn't expose it here due to commercial data concern

Below is my action :



Output from response:
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Hey Bear,

here is my latest progress

from previous result of JSON, which i think is wrong due to it doesn't show pair "[]" in between of JSON data

Previous result:


Latest result with [] included :



by using following method:


Question:
1. It seem like the JSON data being parse to many tokens, as "data.JSONStr[0]" show me value of "[" , but i can't figure out how to get the whole first record from JSON.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Please use proper indentation when posting code.

I would simply do a console.log() of data in the success callback and inspect the result to see of the correct construct is being returned. If not, then you know the returned JSON string is not correctly formed.
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Sorry about the indentation, Bear. I hope it is better now.

Could you please help me to understand in what situation, data.JSONStr[0] become value of "[" , and what is syntax to get the first record of originNode value ?

Between, could you please show me sample of how the correct structure for list of custom object should be ?

Take for instance, there are student list which contain two records of {Name, address, age}.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Again, if you're getting a data value of "[", which is a JSON delimiter, not a value, I don;t think that the JSON is properly formatted. It almost looks as if the JSON string is being passed though a method that is escaping the quotes. So I don;t think that the problem is on the client, but on the server.

Use the JavaScript console to look at the raw value being returned as the response. In Chrome, that would be under the Network section.
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 189
Hey Bear,

you are right on the JSON structure which is incorrect form, it was due to my backend action which miss out some getter/setter method for the custom object before send back to client.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to confirm the JSON return correctly and handle it from given format.