2011-07-22 20 views
0

こんにちは誰もが私のJSONオブジェクト内のiもJSONオブジェクトを持つ配列を持っているということです。問題エンコーディングと解析をJSON

<?php 

if ($_SERVER['HTTPS'] == 'on') { 
    require '../../Objects/Course.php'; 
    require '../config.php'; 
    require '../Tools.php'; 

    $response; 

    $c = new Course(); 
    $courses = listCourses(); 

    if (is_array($courses)) { 
     if (count($courses) != 0) { 
      $response = array('result' => true, 'data' => 'exist', 'courses' => ''); 
      foreach ($courses as $course) { 
       $response['courses'][] = json_encode($course); 
      } 
      print json_encode($response); 
     } else { 
      $response = array('result' => true, 'data' => 'empty'); 
      print json_encode($response); 
     } 
    } else { 
     $response = array('result' => false, 'data' => 'empty'); 
     print json_encode($response); 
    } 
} 
?> 

、これが私のjavascriptです:..when私は、これは私のPHPコードであるJSONオブジェクト

を彼らは文字列として解釈されているので、私は未定義の取得、アレイ内のJavaScriptからJSONオブジェクトにアクセスしようとしませコード:

$.post('ServiceProviders/CourseRelated/listAllCourses.php', function(data){ 
     if(data.result == true){ 
      for(var i = 0 ; i < data.courses.length ; i++){      
       alert(typeof data.courses[0]); 
      }     
     }else{ 
      alert('something went wrong while listing all the courses...try again later.') 
     } 
    },'json'); 

警報の出力ではなく、オブジェクトの文字列である...私はそれをどのように修正するのですか?

答えて

1

はあなただけJSONに一度、あなたの応答をエンコードしたいので、$response['courses'][] = $course;$response['courses'][] = json_encode($course);を交換してみてください。