简体   繁体   English

在php jquery中的json对象中获取密钥

[英]Fetch key inside a json object in php jquery

I am returning a json data to jquery using php. 我正在使用php将json数据返回到jquery。 what i want is to access the keys inside the json. 我想要的是访问json中的键。 i followed a tutorial and used json stringify but im not able to access the keys. 我遵循了一个教程,并使用json字符串化,但无法访问密钥。

json data : json数据:

[
    {"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: jQuery中的代码:

var json = data;
var obj = JSON.stringify(json);
console.log(obj[1].id);

you have to use JSON.parse(json). 您必须使用JSON.parse(json)。 then, you can do it. 然后,您可以做到。

JSON.stringify() will take your JSON object and convert it to a JSON string (here which is response). JSON.stringify()将获取您的JSON对象并将其转换为JSON字符串(此处为响应)。

JSON.parse() will take stringified JSON and convert it to object so that you can access via . JSON.parse()将采用字符串化的JSON并将其转换为object,以便您可以通过进行访问. 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.... 我希望你明白了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM