简体   繁体   English

JSON.stringify(data)填充变量

[英]JSON.stringify(data) populate variable

I have data from my model coming back to me. 我有模型的数据返回给我。 In the past i have used 过去我用过

$.parseJSON(data); 

This I would then use $.each and loop through the data and output arrays into tables rows/columns. 然后,我将使用$ .each并遍历数据和输出数组到表的行/列中。

Instead what I want to do is have a number of variable in which I can decide whether to set to value of a textbox or div or any element 相反,我要执行的操作是具有多个变量,在其中可以决定是否将其设置为文本框或div或任何元素的值

I was seeing this JSON.stringify and wondered about if it is more practical in that it is putting the data into a string, except I wasn't sure HOW to really use it. 我看到这个JSON.stringify并想知道它是否更实用,因为它会将数据放入字符串中,除非我不确定如何真正使用它。

JSON.stringify(data)

Example: 例:

<li><a href="#">USERID</a></li>

OR 要么

<input type=text id="whatever">

I can see in chrome dev tools output of data is 我可以在chrome dev工具中看到的数据输出是

Object {ntid: "bthorn", AuthGroup: "admin", DOCCCOGroup: "sst"}

What is the easiest way to JUST get that "ntid" value? 仅仅获得“ ntid”值的最简单方法是什么?

var ntid = data["ntid"] ???   
alert(data.ntid)

DEMO 演示

Use the key of the value you want to get 使用您想要获得的值的键

$.parseJSON is the same as JSON.parse not JSON.stringify the former parses a JSON string into a javascript object, the latter converts a javascript object into a JSON string. $.parseJSONJSON.parse相同,不是JSON.stringify ,前者将JSON字符串解析为javascript对象,后者将javascript对象转换为JSON字符串。

And answering your question you could access using data.ntid or data["ntid"] 回答您的问题,您可以使用data.ntiddata["ntid"]

var json = '{ "foo": "bar", "test": "stack" }';
var obj = JSON.parse(json); // obj = { foo: "bar", test: "stack" };

var obj = { foo: "bar", test: "stack" };
var json = JSON.stringify(obj); //'{ "foo": "bar", "test": "stack" }'

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

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