Solved

Newbie to JSON use from javascript - JSON iteration and getting values

Posted on 2007-12-05
2
915 Views
Last Modified: 2010-04-21
Hello,

I am new to JSON and I am using the prototype library by the way.

the data that a recieve from my ajax query looks like this
{'0':'string1','1':'string2'}

I will not know any of the elements at query time but what I do need to do is find out the number of elements and to be able to iterate throught them to get all of the values.

How do I do that using JSON notation.

Thanks,

David


0
Comment
Question by:dmontgom
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 20416107
<script type="text/javascript">
var result=null;

//...
eval("result=" + JSONString + ";");
if(result)
{
 for( var i in result )
  alert( i + "=" + result[i] );
}
where the "..." is the code that makes the request to the server and JSONString is the data you are getting from the server in JSON format.
0
 

Author Closing Comment

by:dmontgom
ID: 31413000
Way to go....thanks for the quick response.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now