?
Solved

JSON encode-decode problem

Posted on 2009-05-19
8
Medium Priority
?
530 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

801 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