aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Parsing this JSON String array using Jquery displays no message in chrome debugger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

Parsing this JSON String array using Jquery displays no message in chrome debugger

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I am trying to parse and display this simple JSON :



I am using jquery-1.10.2.js

However this does not give any response on the HTML page or any error message in chrome debugger. Below is my HTML Page:






I tried debugging this using chrome debugger but it gives not error message nor response.
Please advice.Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Why is your code formatted so oddly? The $ signs on their own lines? The excessive indentation? It makes the code just as hard to read as if there is no formatting.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Sorry for that. This formatting had happened only after clicking autoformatting option of Eclipse Cntrl Shift F. It works fine for java code but for javascript code it all got scattered.I corrected the formatting and am posting it below:


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Have you set breakpoints to see if the code is doing what you think it is. For example, right after you create div_data (which by convention should be divData) have you checked that what it contains is correct?
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks. instead of breakpoints I was trying to do debugging by putting alters such as alert(div_data) but it did not give anything.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Alerts are a phenomenally poor way to debug. At minimum, use console.log() to do logging. But for this type of issue, you cannot beat breakpoints to halt the code and give you a chance to look around and see what's actually happening.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
thanks.I will do this. Should we not do eclipse autoformating (cntrl shift F) on a file with Javascript code too?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Looking at how awful it formats the code, can you not answer this yourself?
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Yes. It works well for other code like java class files or HTML or JSP files but for javacript files it for some reason scatters the code.I will avoid it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Or tune it.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I have observed that my JSON is different from normal JSON.

My JSON. is :

{"stringarray1":["A1","B1","C"]}


If the JSON were

{"stringarray1":["A1":"Z1","B1":"Y1","C":"X1"]} , then it is understood that A1 is key Z1 is value B1 is key Y1 is value and so on.


But in case of my JSON there are no key value pairs like ("A1":"Z1").
In case of my JSON if A1 is key then what is the value or if it is value then what is the key.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

There are no keys in an array.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
But JSON defination says that "JSON data is written as name/value pairs." so where are name value pairs here?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

defination is speclled "definition"

And no, that is inaccurate. There are no keys in arrays.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I have changed the Javascript code as below but now I am getting a message "Uncaught TypeError: Object [object Object] has no method 'live'"



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Why do you have a document ready function inside another function? And what does this have to do with your original question?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

You may want to look here, bearing in mind you are using version 1.10.2:

http://api.jquery.com/live/
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
And what does this have to do with your original question?




All I have been trying to achieve is parse and display JSON of type {"stringarray1":["A1","B1","C1"]} from ajax request from source 'http://localhost:8080/TempApp/JSONDataAction1.action'


Why do you have a document ready function inside another function?


I will recheck my javascript code.



Will try this.


Thanks all.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I have done changes in my java script as below but still it gives no response even in debugger:


James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

The debugger is not going to give you a response.

Have you used it to examine the contents of the collection you are attempting to display? Does your function even get executed? Is there another condition which has an unexpected value which results in nothing being displayed?

All questions that the debugger should answer for you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

I agree with James, and as I have advised you earlier in this topic, use the debugger to set breakpoints and see what's going wrong. Just posting code change after code change and asking us what is wrong is going to take hundreds of times longer.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Sorry for that.

I put a beakpoint. It gives a message "ReferenceError: data is not defined"
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I have put a breakpoint at line



but it does not reach here. When I had put at line before that it was reaching there but not until this line.



Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Using debugger breakpoints I have isolated that the problem area is on below line because it is executing as expected till this line:



My Code:
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Your last two posts appear to contradict each other.

You say that a particular line is not being executed but then the next post says the same line is the problem.

Stop changing the code and tell us:

1) The error you are seeing
2) On what line the error occurs (bearing in mind that if the line is not executed, it is not the problem.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
The issue has been isolated to a bigger extent now as can be seen below in the post under "Observations"

Boswell says
Your last two posts appear to contradict each other.

You say that a particular line is not being executed but then the next post says the same line is the problem.

Stop changing the code and tell us:

1) The error you are seeing
2) On what line the error occurs (bearing in mind that if the line is not executed, it is not the problem.


Earlier the control was not going till that line in debugger but I isolated it there was a mistake with URL I had given in that line. I corrected it.

Now control goes until the line



OBSERVATION FROM CHROME DEBUGGER:

There is a useful observation from chrome debugger.

In the line
If I check the value in jd.items it shows


stringarray1: Array[3]
0: "A1"
1: "B1"
2: "C"

Above is the value in jd.items.

Now it means till here the value is coming correct and surely the only issue is some mistake in displaying it.


PROBLEM AREA ISOLATED:

Surely there is some problem in the second line because untill the first line value is coming correct..




thanks
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I have taken help from debugger to isolate the problem area and the entire issue comes down to the second line in below:

$.each(jd.items, function(i,item){
$('#stage').append('<p>' + item + '</p>');
});

What is the issue with the second line of code and how to correct it. If i check value in jd.items it shows in debugger as

stringarray1: Array[3]
0: "A1"
1: "B1"
2: "C"

which is correct.So the only question that remains now is that what needs to be corrected in the second line above.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
The issue is finally Resolved.

Thanks.


The below code worked fine for me:


Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks. My issue is Resolved.

However I am trying one more thing. The JSON array which is returned, I want it to happen dynamically. Which JSON array is to be returned should be decided dynamically. So parameter should be sent with getJSON array to Struts2 Action and from Struts2 Action it should decide which JSON array to be returned.

Now I know that this also involves Struts2 so I might need to ask this in Struts 2 Forum. So I am now seeking help in struts2 forum.

Thanks again for resolving my original issue.
 
 
subject: Parsing this JSON String array using Jquery displays no message in chrome debugger