weixin_33735077 2012-01-07 01:58 采纳率: 0%
浏览 118

在json数据上使用eval()

I'm trying to use eval on a json formatted dataset to retrieve the value of an attribute i.e.,

[{"item_one":1, "item_two": 2, "item_three": 3}]

I'm using dajax for the ajax call within django like this:

@dajaxice_register
def get_current_id(request):
    home_timeline = oauth_req(
    'http://api.twitter.com/1/statuses/home_timeline.json?count=1',
    settings.TWITTER_TOKEN_KEY,
    settings.TWITTER_TOKEN_SECRET
    )
    return simplejson.dumps({'home_timeline': home_timeline })

which gets this as it's callback:

function get_current_id_callback(data){
      if(data==Dajaxice.EXCEPTION){
        alert('Error! Something happens!');
      }
      else{
        console.log(data.home_timeline);
      }
    }

I don't understand why this gives me undefined:

console.log(eval(data.home_timeline.item_one))

what's the correct way to retrieve the value of item_one?

Thanks.

  • 写回答

1条回答 默认 最新

  • 关注

    If data is still a string, then you need to eval(data) (or as @cHao suggests, parse it) to yield a javascript object capable of being dereferenced.

    If it's not just a string, it should not be eval'd first.

    评论

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline