loop through list of objects

I'm doing JQuery   AJAX call to server.  Server is returning Json.  Here is the pseudo code

Server Side code
-------------------------------
................................................
// create a new Gson instance
 Gson gson = new Gson();
// make a cartList.  cartList is a list of Product objects
 // convert your list to json
String jsonCartList = gson.toJson(cartList);  
 // return your generated json
out.print( jsonCartList);  // return to UI


UI Side
------------------------
What code I will write to get the  array of  Products here ?    I am using Jquery

    $.ajax({
      url:"getvalue",
      dataType: "json"
      success:function(responseData) {
         // I'want to loop through list of Products returned by the server. what code I'll write here ?
      }
   });
cofactorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cofactorAuthor Commented:
comments please
0
gurpsbassiCommented:
But a breakpoint in the success handler and inspect what the responseData parameter contains
0
cofactorAuthor Commented:
@gur,
Please look at my query and code again. I need a workaround.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Rainer JeschorCommented:
Hi,
could you please post / attach the JSON response. Without knowing the response structure we can't deliver a correct solution.
Thanks
Rainer
0
cofactorAuthor Commented:
I made some changes in my code.
For some reason I am using dataType: "text"  (not "json" )

code and query attached  .
location
0
gurpsbassiCommented:
are you sure that is the correct response?

Your original question said you wanted to loop through the list of products. They don't look like products to me.
0
cofactorAuthor Commented:
yes...thats the correct response....I made some changes to the code.  
Do you have any answer to that ?
0
RobOwner (Aidellio)Commented:
http://jsbin.com/vowozi/1/edit?js,console

you have to set the type to "json" and jquery will parse it into an array of json objects for you
0
cofactorAuthor Commented:
>>you have to set the type to "json"

No. I want to keep that in "text"  for some reason .....given that how do I convert the given response into array of json objects ?
0
RobOwner (Aidellio)Commented:
use JSON.parse(result)

e.g.

var encryptedString = "";

$.ajax({
  type: 'GET',
  url: 'http://jsbin.com/gilazi/1.js',
  data: { ParentlocationId: encryptedString ,locationFlag : "STATE"},
  dataType: 'text', // or 'text' or 'html'
  error: function (result) {

  },
  success: function (result) {
    console.dir(JSON.parse(result));
  }

});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rainer JeschorCommented:
Hi,
and back to your question:
this will loop through each item:
$.ajax({ 
	type: 'POST', 
	url: 'getList.json', 
	data: { 
		ParentlocationId: encryptedString ,
		locationFlag : "STATE"
	}, 
	dataType: 'text', 
	error: function (result) { }, 
	success: function (result) { 
		var resultAsJSON = jQuery.parseJSON(result);
		$.each(resultAsJSON, function() {
			// "this" is the current item e.g.
			console.log(this.locationId);
		});
	}
});

Open in new window

HTH
Rainer
0
RobOwner (Aidellio)Commented:
Thanks Rainer, given the question was to iterate through the objects *grin*
0
Rainer JeschorCommented:
Hi Rob,
hopefully it ends with your solution and my assisted splitting the points as the question is indeed ambiguous - I did not "see" the issue related to create the objects array :-)
0
cofactorAuthor Commented:
That solved my problem perfectly. Thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.