• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

how do i iterate a returning json string  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i managed to return a JSON string like this



when i print this response in my javascript in prints this:



i'm using jquery and this is my code so far when i'm trying to get access to the data inside my returning JSON:



but it give's me this error



inside my JSON are the first object is a POJO Perm object, the other object is an ArrayList<Roles> this list contains two (2) roles, but o dont know why is printing is there are 2 objects in my json 3 in total if you count the two objects that contains the list but the whole list shouldn't be one (1) object or json counts all the objects in the list separately,

my question is how should i iterate over this json to get the values inside.
 
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing where? And how? From Java? Or JavaScript?
 
Jhon Parker
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to print them using console.log(), in the javascript function searData() i posted, also all the text and errors i said where printed in my javascript function using console.log(), i whatched those errors in my chrome console and i used those console.log() to debug my code and see what's wrong with it, i wanted firts to print the values of my returning json in my javascript to later use them properly
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The this has nothing to do with Java at all? I've moved it to the HTML/JavaScript forum.
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
console.log() is a diagnostic tool, not a print engine. Different dev tools will represent objects in different ways. Why are you concerned with exactly what it prints out?

In many dev tools, you can expand the object to see its contents. You can also type in expressions at break points to see what variables and objects contain.
 
Bartender
Posts: 1810
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you creating the JSON? Please post the servlet code that constructs and returns the JSON, or if you have a separate object that constructs it for the servlet, post that.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, the JSON string you posted isn't valid JSON. So it's not surprising that you're getting errors when trying to work with it. Send it through a validator such as http://jsonlint.com/ for clues about what's invalid.

Looks like it might be as simple as missing a closing "]". But then, you have an array with two elements. The first element is another array that contains two objects. The second element is an object. Are you sure this is the structure you want?

You mentioned the Chrome console. Another part of the Chrome dev tools useful for this kind of debugging is the Network tab. There should be a list of the page's requests, including your Ajax request. If you don't see tabs for the response to this request, click on the request URL to display them. The Response tab will give you the response as a text string, and the Preview tab will give you an expandable object that you can drill into to check values and structure (as long as the JSON is valid, I would presume).
 
Jhon Parker
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the method where i create the JSON



here is the controller i'm using Spring MVC:



you where right the json was not valid here is my json after your opinion



here is what i have tried so far:



So far i had managed to print the firts object that is the Perm Pojo object, and also i'm able to print some fields of the objects that are inside the ArrayList<Role> like name,description,type and state, but i'm having troubles when i try to print the permList list inside my Role object that is inside the ArrayList<Role>, it says this

Cannot read property 'name' of undefined

in this line



also if i try to see the lenght of the list like this



it prints that the lenght is 0

here is my Role class



and my Perm Class



i dont pass the DAO methos since they are already teste and they work fine i have debugged them and they return the data fine, when i call the in my controller class, the roleList have roles with their respective permList with the correct data, the permList is not empty, the permList is full with tehir respective perms.
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have not answered the question as to why you are doing this in the first place. What's wrong with using the console as intended?
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. And you are aware of console.dir(), right?
 
Jhon Parker
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You still have not answered the question as to why you are doing this in the first place. What's wrong with using the console as intended?



i'm not aware of console.dir()

and what do you mean when you say: "why you are doing this in the firts place"?? how are you suppose to use the console as intended?


i don't know if this answer your question, but i'm doing this because i'm creating a delete permision page, and in this page there's a select when you select the permision and when you do this, it loads the information of the perms in some html labels, also i need to load a select with the roles in the systme that have that permission, that's why i retun in the json a list of Roles, but i'm having torubles when i tried to get the field permList in the Roles objects inside the list.
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but what has that to do with trying to change what console.log() outputs?

The first thing to do is to fix your JSON. As already pointed out, it makes no sense to have nested lists the way you have.

Also, are you generating the JSON by hand? If so, don't. Let one of the established and debugged JSON libraries generate valid JSON for you.
 
Jhon Parker
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:OK, but what has that to do with trying to change what console.log() outputs?

The first thing to do is to fix your JSON. As already pointed out, it makes no sense to have nested lists the way you have.

Also, are you generating the JSON by hand? If so, don't. Let one of the established and debugged JSON libraries generate valid JSON for you.



1) i'm not rying to change anything that the console.log() outputs hahaha where did you get that idea??

2) i'm using jackson to generate my JSON, and why do you say that my list don't hve any sense?


3) i checked again and it was my fault sry for wasting your time friend it was my DAO method that was returning the roles without their permlists, sorry friend, but i'm still having prolbems when i do this :

in the second for it says "undefined" it looks like it's trying to find a property in my class named lenght, and i think it's not looking for the function "lenght" persee, but my big question is how this works in the third 3 "for" since it loop the permList fine?




i'm talking about this "for"

this .lenght works



but this .lenght dont work



here is the whole loop
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that in addition to writing the code that consumes the response, you're also writing the code that processes the request and formats the response. This is a luxurious position to be in -- you get to craft the two to work together exactly as you see fit. So from my standpoint, why are you outputting the data in such an odd format, which is causing you to write torturous, error-prone code with all those i's and k's and u's?

You want to populate two lists with stuff, right? Make it easy on yourself. At the highest level, have two named variables each which point to an array of objects containing your stuff. Then all you initial traversal (and null-checking) of the structure is by named fields.



Better, I hope?

Edited to add: And if those inner lists are just strings or some other primitives and not objects, things are even simpler:


 
Jhon Parker
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Matola wrote:It appears that in addition to writing the code that consumes the response, you're also writing the code that processes the request and formats the response. This is a luxurious position to be in -- you get to craft the two to work together exactly as you see fit. So from my standpoint, why are you outputting the data in such an odd format, which is causing you to write torturous, error-prone code with all those i's and k's and u's?

You want to populate two lists with stuff, right? Make it easy on yourself. At the highest level, have two named variables each which point to an array of objects containing your stuff. Then all you initial traversal (and null-checking) of the structure is by named fields.



Better, I hope?




hi friend that looks more clean hehe, but since i'm not creating the JSON by hand, i'm using JACKSON and using this way to create the JSON



i map the two objects using that function "writeValueAsString" and then i concat them in a final string "String JsonResponse= "[" + permData+ "," + roleListData+ "]" , i would like to doit like you post in that example but how can i create a JSON like that using JACKSON and not making it by hand?
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!