[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

JSON encode-decode problem

Posted on 2009-05-19
8
Medium Priority
?
537 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
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!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 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