duanlaiyin2356 2014-01-26 06:18
浏览 41

无法在javascript中解析多个json行

I am using twitterapi to get the friends list in php and I have encoded the result as a json array, but I cannot parse the json array in javascript.I have validated the json array produced by the php and its a valid json array. Below is my code.

php

$friends = array();
$friend_list = array();
$myfriend = array();
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET,oauth_token,oauth_token_secret);
 $friends =$connection->get("https://api.twitter.com/1.1/friends/list.json?cursor=-1&screen_name=twitterapi&skip_status=true&include_user_entities=false&count=200);

foreach($friends as $friend) {
           if(!empty($friend))
           {
           foreach($friend as $value)
           {
                $friend_list['id']=$value->id; 
                $friend_list['screen_name']= $value->screen_name;
                $friend_list['name']= $value->name;
                $friend_list['profile_image_url']= $value->profile_image_url;
                $friend_list['location']= $value->location;
                array_push($myfriend, $friend_list);

            }

           }
       } 

  $newarray = json_encode($myfriend);

'

javascript

<script>
var obj1 = JSON.parse('<?php echo $newarray ;?>');

console.log(obj1); // am not getting anything in console
</script>

EDITED

output from echo $newarray;

[
    {
        "id": 50393960,
        "screen_name": "BillGates",
        "name": "Bill Gates",
        "profile_image_url": "http://pbs.twimg.com/profile_images/1884069342/BGtwitter_normal.JPG",
        "location": "Seattle, WA"
    },
    {
        "id": 141527741,
        "screen_name": "prakashraaj",
        "name": "Prakash Raj",
        "profile_image_url": "http://pbs.twimg.com/profile_images/2951815972/ab32fb806b480d0dc761805ae4ef9775_normal.jpeg",
        "location": "india"
    },
    {
        "id": 88856792,
        "screen_name": "aamir_khan",
        "name": "Aamir Khan",
        "profile_image_url": "http://pbs.twimg.com/profile_images/2254031972/_MG_2190_normal.jpeg",
        "location": "Mumbai"
    },
    {
        "id": 107318424,
        "screen_name": "bipsluvurself",
        "name": "Bipasha Basu",
        "profile_image_url": "http://pbs.twimg.com/profile_images/419745345178832896/8JvqwEM9_normal.jpeg",
        "location": "Mumbai, India"
    }
]

Please help, am stuck with this

  • 写回答

2条回答 默认 最新

  • dregduc63699 2014-01-26 06:23
    关注

    In the absence of information about how you validated json array, this is what I can recommend. Change your javascript to:

    <script>
    var jsonString = '<?php echo $newarray ;?>';
    console.log(jsonString);
    var obj1 = JSON.parse(jsonString);
    console.log(obj1); // am not getting anything in console
    </script>
    

    You can see the content of jsonString in the javascript console. That should give you hints about what is going wrong.

    Note:You are fetching JSON content from twitter, converting it to PHP data structure and converting it back to JSON. Sending the JSON string from twitter to javascript is far more efficient - if there is no need to filter/alter the twitter returned data.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog