www.webdeveloper.com
Results 1 to 8 of 8

Thread: json text to javascript object

  1. #1
    Join Date
    Apr 2010
    Posts
    22

    json text to javascript object

    I am retrieving text back from a php script which has data in json form.

    My goal is to take that text and store it into an object.

    This is how I currently have it set up and is not working.
    Code:
    var jsonText = xmlhttp.responseText;
    						
    alert(jsonText);
    						
    var employeesArray = JSON.parse(jsonText);
    alert(employeesArray);
    var employeeList = new array();
    						
    for(l = 0; l < employeesArray.length; l++) {
            var employeeObj = new Employee();
            employeeObj.id = employeesArray[l].id;
            employeeObj.firstname = employeesArray[l].firstname;
    	employeeObj.lastname = employeesArray[l].lastname;
    	employeeObj.email = employeesArray[l].email;
    	employeeObj.location = new Location();
    	employeeObj.location.id = employeesArray[l].location.id;
    	employeeObj.location.name = employeesArray[l].location.name;
            employeeObj.company = new Company();
            employeeObj.company.id = employeesArray[l].company.id;
    	employeeObj.company.name = employeesArray[l].company.name;
    	employeeObj.position = new Position();
    	employeeObj.position.id = employeesArray[l].position.id;
    	employeeObj.position.name = employeesArray[l].position.name;
    							
    	employeeList.push(employeeObj);
    							
    }
    The text from the first alert is
    [{"id":"31","firstname":"fname1","lastname":"lname1","email":"testemail1@gmail.com","company":{"id":" 1","name":"Test Company"},"location":{"id":"2","name":"Broadway"},"position":{"id":"6","name":"CEO"}},{"id":"3","fir stname":"fname2","lastname":"lname1","email":"testemail2@gmail.com","company":{"id":"1","name":"Test Company"},"location":{"id":null,"name":null},"position":{"id":null,"name":null}}]
    The text from the second alert is
    [object Object],[object Object]
    also the for loop is not iterating

    Can anyone see a problem?

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Code:
    var employeeObj = new Employee()
    I see no Employee constructor anywhere.
    maybe you should create the object like:
    Code:
    var employeeObj= function(){}
    Moreover:
    Code:
    var employeeList = new array();
    maybe new Array(). Or even better:
    Code:
    var employeeList=[];
    Last edited by Kor; 04-01-2011 at 03:29 PM.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    the alerts look as they should, [objectObject] is what objects are supposed to look like inside an alert...

  4. #4
    Join Date
    Apr 2010
    Posts
    22
    Ok thats a relief. I don't understand still why the look is not looping. Clearly there are two objects in the employeesArray variable.

  5. #5
    Join Date
    Mar 2009
    Posts
    464
    I'm not sure I understand exactly what you are doing or why but if you structure it right objects in PHP (or at least their properties) can be easily translated into Javascript objects through the JSON format. Use the native json_encode function with the PHP object and transmit it. On the receiving side, (I'm assuming a response to an AJAX call) take the response text and simply do something like: var x = response_text.

    Consider this:

    PHP code:
    Code:
    class blank{
    public $a;
    };
    $v = new blank();
    
    $v->a = array("1","2","3");
    
    $v = json_encode($v);
    echo $v;
    Now when the browser receives it as responseText, in Javascript,
    you simply put:
    Code:
    var myJavascriptVar = responseText;
    You will then find, as long as you kept to arrays, hashes, and primitives in your original PHP object, that as a data structure myJavascriptVar will mirror in Javascript what it did in PHP, and can be manipulated in much the same way, as in:

    Code:
    alert(myJavascriptVar.a[2])
    which should yield '2,' as in the original PHP object's value for that offset to the array.

  6. #6
    Join Date
    Apr 2010
    Posts
    22
    Thanks Tcobb. The main problem is why the loop is not being implemented. The variable employeesArray clearly is an array for two objects, so why the loop does not recognize that, is beyond me.
    Last edited by rickburgen; 04-02-2011 at 11:57 AM.

  7. #7
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    The error that Kor pointed out would have crashed your script just before the for loop. Did that ever get fixed?
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  8. #8
    Join Date
    Apr 2010
    Posts
    22
    YES...The issue was because array() should be Array(). That just made my day. Thanks Kor for the solution.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles