2018-08-31 15:41
浏览 172

在php jquery中获取json对象内的键

I am returning a json data to jquery using php. what i want is to access the keys inside the json. i followed a tutorial and used json stringify but im not able to access the keys.

json data :

    {"id":"1","movie_name":"spiderman","releases_on":"20th August 2018"},
    {"id":"2","movie_name":"batman","releases_on":"21st August 2018"},
    {"id":"3","movie_name":"fast and furious 6","releases_on":"22nd August 2018"}

code in jquery:

var json = data;
var obj = JSON.stringify(json);

图片转代码服务由CSDN问答提供 功能建议

我使用php将json数据返回给jquery。 我想要的是访问json内的键。 我按照教程使用json stringify,但我无法访问密钥。


“{”“:”1“,”movie_name“  :“spiderman”,“releases_on”:“2018年8月20日”},
 {“  id“:”3“,”movie_name“:”速度与激情6“,”releases_on“:”2018年8月22日“} 


  var json = data; 
var obj = JSON.stringify(json); 
console.log(obj [1] .id  ); 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dony113407 2018-09-02 14:11

    you have to use JSON.parse(json). then, you can do it.

    打赏 评论
  • dongyue6199 2018-09-02 14:27

    JSON.stringify() will take your JSON object and convert it to a JSON string (here which is response).

    JSON.parse() will take stringified JSON and convert it to object so that you can access via . operator

    for example,

    var a = { name: "John", age: 20 }
    console.log(typeof a)   // object
    var out = JSON.stringify(a)
    console.log(typeof out)  // string
    var res = JSON.parse(out) 
    console.log(typeof res)  // object

    I hope you got it....

    打赏 评论