Solved

JSON encode-decode problem

Posted on 2009-05-19
8
518 Views
Last Modified: 2013-11-08
Hi there,
In php i have an object myObject, which has integers, string and arrays. I push all of the instances of myObjects in a simple array, then encode the array with $ious_string = json_encode($places_obj_ious);
I then send this string to javascript via AJAX, and decode it with ious_string.evalJSON() using he prototype framework.
What is the resulted object?
I need to be getting the array i had generated n php. How can i itterate through the collection f objects created?
0
Comment
Question by:ioustinos_sarris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 

Author Comment

by:ioustinos_sarris
ID: 24423242
I am doing this
var temp = ious_string.evalJSON();
While alert(temp[0].id) would show something valid alert(temp.length) is undefined.

So i cant really itterate through the "array"
0
 
LVL 12

Expert Comment

by:alien109
ID: 24423299
can you show the json string that is returned from the ajax call? that would help.
0
 

Author Comment

by:ioustinos_sarris
ID: 24423408
{"2":{"user_name":"Ioustinos Sarris","user_id":"514324456","is_transit":"0","tags":"","image":"http:\/\/profile.ak.facebook.com\/profile6\/1281\/91\/s514324456_7609.jpg","sex":"male","gender":"1","me":1,"created_at":null,"starttime":"1242285300","endtime":"1242318600","networks":[{"nid":"67109257","name":"Greece","type":"region","status":"","year":"0"}],"is_friend":"0","imageSmall":"http:\/\/profile.ak.facebook.com\/profile6\/1281\/91\/q514324456_7609.jpg","id":"300","says":""},"1":{"user_name":"Ioustinos Sarris","user_id":"514324456","is_transit":"0","tags":"","image":"http:\/\/profile.ak.facebook.com\/profile6\/1281\/91\/s514324456_7609.jpg","sex":"male","gender":"1","me":1,"created_at":null,"starttime":"1242266400","endtime":"1242288900","networks":[{"nid":"67109257","name":"Greece","type":"region","status":"","year":"0"}],"is_friend":"0","imageSmall":"http:\/\/profile.ak.facebook.com\/profile6\/1281\/91\/q514324456_7609.jpg","id":"299","says":""},"0":{"user_name":"Ioustinos Sarris","user_id":"514324456","is_transit":"0","tags":"","image":"http:\/\/profile.ak.facebook.com\/profile6\/1281\/91\/s514324456_7609.jpg","sex":"male","gender":"1","me":1,"created_at":null,"starttime":"1242285300","endtime":"1242318600","networks":[{"nid":"67109257","name":"Greece","type":"region","status":"","year":"0"}],"is_friend":"0","imageSmall":"http:\/\/profile.ak.facebook.com\/profile6\/1281\/91\/q514324456_7609.jpg","id":"298","says":""}}
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:ioustinos_sarris
ID: 24423419
the above response is the encoded response for an array with 3 objects...
0
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
ID: 24423686
What's coming back isn't an array of objects, but an object with three key/value properties

to enumerate though this object, you could do the following

for(var key in temp)
{
    var user_name = temp[key].user_name;
    var user_id = temp[key].user_id;
    ...
}
0
 

Author Comment

by:ioustinos_sarris
ID: 24430322
well it seems to be ok but it is not.
while in php the length of my array is 96 objects, and i can see with print_r they are all valid and not empty, when i do
for(var key in temp){
            myarray.push(new place(temp[key]));
      }

i get 134 elements in myarray...
the last 38 are null...
0
 

Author Comment

by:ioustinos_sarris
ID: 24430368
should i try using another encoder?
0
 

Author Comment

by:ioustinos_sarris
ID: 24458491
anyone?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicate Elements with JS / jQuery 3 26
restriction of entering a a page 5 34
Read text on Table 7 30
isNaN issue on the Table input text 2 11
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

730 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