Solved

JSON encode-decode problem

Posted on 2009-05-19
8
503 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
 

Author Comment

by:ioustinos_sarris
ID: 24423419
the above response is the encoded response for an array with 3 objects...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 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)

707 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

17 Experts available now in Live!

Get 1:1 Help Now