PHP json_encoding没有正常运行

时间:2016-11-18 14:19:25

标签: php

当我从数组转换为json时,它无法正常工作

php code

$url = 'api link'; // It's not correct link
    $ch = curl_init();      
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            curl_close($ch);
          echo json_encode($response);

数组结果

Array ( [0] => m_vijay [1] => l_rahul [2] => c_pujara [3] => v_kohli [4] => a_rahane [5] => r_ashwin [6] => w_saha [7] => r_jadeja [8] => j_yadav [9] => u_yadav [10] => s_ahmed )

我的输出结果

"Array\n(\n [0] => m_vijay\n [1] => l_rahul\n [2] => c_pujara\n [3] => v_kohli\n [4] => a_rahane\n [5] => r_ashwin\n [6] => w_saha\n [7] => r_jadeja\n [8] => j_yadav\n [9] => u_yadav\n [10] => s_ahmed\n)\n"

我试过很多次我都没有得到它。请帮帮我

2 个答案:

答案 0 :(得分:0)

当您尝试编码编码时会发生这种情况:) 试试这个:

print_r(json_encode((object)$response));

我希望它有所帮助

答案 1 :(得分:0)

您的响应不是数组,它是由print_r格式化的字符串,通常所请求的字符串必须像json一样格式化。请先用var_dump()回复它。