简体   繁体   English

如何使用json数组键转换为javascript变量

[英]how to use json array key to javascript variable

i am try json_encode($updateArray); 我正在尝试json_encode($updateArray);
and this is call by ajax and after ajax success 这是由ajax调用,并且在ajax成功之后
return me json string on responseText something like this type 在responseText上向我返回json字符串,类似于这种类型

{"title":"superAdmin","id":"50"}

now i want to use this two key 现在我要使用这两个键
like 喜欢

var text = title;
var id   = id;

who can i user this two to as diff. 谁可以使用这两个作为差异 variable. 变量。
thanks. 谢谢。

You parse the responseText into an object graph using JSON.parse : 您可以使用JSON.parseresponseText解析为对象图:

var result = JSON.parse(xhr.responseText);

All modern browsers have JSON.parse now (but not IE7 and earlier; for them, you can use a library like the ones Crockford has on his github page .) 所有现代浏览器现在都具有JSON.parse (但IE7和更早的版本不具有JSON.parse ;对于它们,您可以使用Crockford在其github页面上拥有的库 )。

Once you have the object graph (which is just a single object, in your case), you can get the information: 一旦有了对象图(在您的情况下,它只是一个对象),就可以获取信息:

var title = result.title;
var id = result.id;

Use JSON.parse : 使用JSON.parse

var json = '{"title":"superAdmin","id":"50"}';
var obj = JSON.parse(json);

And now, you can do: 现在,您可以执行以下操作:

var text = obj.title;
var id = obj.id;

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

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