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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
thanks.I will do this. Should we not do eclipse autoformating (cntrl shift F) on a file with Javascript code too?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Looking at how awful it formats the code, can you not answer this yourself?
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Or tune it.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There are no keys in an array.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
But JSON defination says that "JSON data is written as name/value pairs." so where are name value pairs here?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
defination is speclled "definition"

And no, that is inaccurate. There are no keys in arrays.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why do you have a document ready function inside another function? And what does this have to do with your original question?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have done changes in my java script as below but still it gives no response even in debugger:


 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Sorry for that.

I put a beakpoint. It gives a message "ReferenceError: data is not defined"
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The issue is finally Resolved.

Thanks.


The below code worked fine for me:


 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic