Solved

JSON encode-decode problem

Posted on 2009-05-19
8
513 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
  • 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 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