如何将变量解析为JSON对象(Javascript) [英] How to parse variables to JSON Object (Javascript)

查看:62
本文介绍了如何将变量解析为JSON对象(Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将外部变量解析为JSON对象。

I want to be able to parse external variables into a JSON object.

这样的事情可以:

var username = "zvincze";

使用该变量解析JSON对象:

And using that variable to parse a JSON object:

var json = '{"username":"VARIABLE_GOES_HERE"}'
var obj = JSON.parse(json);

这是一个非常简单的问题,我似乎无法弄清楚。事实上,我甚至不知道是否有办法实际实现这样的事情。

This is a very simple question that I can't seem to figure out. In fact, I don't even know if there's a way to actually acheive something such as this.

推荐答案

你需要区分仔细地在JSON和JavaScript对象之间。它们是两个完全不同的东西。

You need to distinguish carefully between JSON and JavaScript objects. They are two entirely different things.

JSON是一种与语言无关的基于字符的数据表示,用于数据交换。例如,如果要将数据发送到服务器,则需要采用字符串形式,因此您需要将数据转换为JSON。如果您从服务器接收数据并且它是JSON格式,则需要将其转换为JavaScript对象,以便在JavaScript程序中使用它。

JSON is a language-independent character-based data representation that is used for data interchange. For instance, if you are sending data to a server, it needs to be in string form, so you would need to convert your data into JSON. If you are receiving data from a server, and it is JSON format, you need to convert it into a JavaScript object so you can use it in your JavaScript program.

JSON实际上与JavaScript无关。例如,JSON可以用作交换数据的Java程序的基于字符串的数据表示。 Java和几乎所有其他语言都提供了自己的库例程,用于将其本机数据转换为JSON,反之亦然。

JSON actually has nothing to do with JavaScript. For example, JSON could be used as a string-based data representation for Java programs exchanging data. Java and pretty much all other languages provide their own library routines for converting their native data into JSON and vice versa.

JavaScript中使用的JSON和JavaScript对象之间的唯一关系是JSON字符串看起来非常类似于JavaScript对象文字,还有一些额外的限制,例如必须引用键,它不能保存某些数据类型(如函数),并且它不能保存循环引用。

The only relationship between JSON and JavaScript objects used in JavaScript is that a JSON string looks much like a JavaScript object literal, with some additional restrictions, such as that keys must be quoted, that it cannot hold certain data types such as functions, and that it cannot hold circular references.

JSON的JS部分并不表示JSON是基本的JavaScript数据结构,或者JSON在JavaScript中用于表示或操作数据。它只是来自这样一个事实:如上所述,JSON字符串的格式是在JavaScript对象文字的格式之后松散建模的。

The "JS" part of "JSON" does not indicate that JSON is a basic JavaScript data structure, or that JSON is used in JavaScript for representing or manipulating data. It merely comes from the fact that, as mentioned above, the format of a JSON string is loosely modeled after that of a JavaScript object literal.

在正常的日子里一天JavaScript编程,你永远不会使用JSON,也不需要担心它。正如我所说,当您将数据发送到某个地方或从某处接收数据时,您需要担心JSON ,这需要JSON作为数据格式。当然,在进行ajax调用时就是这种情况,但实际上,即便如此,因为jQuery的 $。ajax 会自动将数据转换为JavaScript对象,如果你告诉它正确的类型,即使在这种情况下,您通常也不必担心JSON。

In normal day-to-day JavaScript programming, you will never use JSON, and don't need to worry about it. You will need to worry about JSON only when, as I said, you are sending data to somewhere, or receiving data from somewhere, that expects JSON as the data format. That is the case when making ajax calls, of course, but actually, even then, since jQuery's $.ajax will automatically convert data to and from JavaScript objects if you tell it the right types, even in that case you do not normally need to worry about JSON.

请注意JSON可以表示原始值的方式,而不仅仅是JavaScript对象。例如,1是有效的JSON,代表数字1。

Note by the way that JSON can represent primitive values, not just JavaScript objects. For example, "1" is valid JSON which represents the number 1.

据我了解你的问题,你想要创建一个JavaScript对象(不是JSON),它包含变量 username 的值之一。您使用常规的旧JavaScript对象文字格式创建JavaScript对象,在这种情况下

As I understand your question, you want to create a JavaScript object (not JSON) which contains as one of its values that of the variable username. You create a JavaScript object with regular old JavaScript object literal format, in this case

{username: username}

这意味着,创建一个对象,其中包含一个名为username的单个键(如果你愿意,请引用它),其值是变量用户名的值。同样,除非你打算在需要JSON字符串表示的地方发送这个对象,否则在这种情况下甚至不需要考虑JSON。

This means, create an object, with a single key named "username" (quote it if you please), whose value is that of the variable username. Again, unless you plan to send this object somewhere that expects a JSON string representation, there is no need to even think about JSON in this case.

理论上,是的,正如一个答案建议的那样,您可以创建一个值为 username interpolated的JSON字符串,使用字符串算法,如'{username: '+ VARIABLE_GOES_HERE +'}',然后使用 JSON.parse 将该JSON字符串转换为JavaScript对象。但是,当您可以简单地将JavaScript对象编写为普通的JavaScript对象文字时,绝对没有理由跳过这些箍。

In theory, yes, as one answer suggested, you could create a JSON string with the value of username "interpolated", using string arithmetic like '{"username":"' + VARIABLE_GOES_HERE + '"}', and then convert that JSON string into a JavaScript object using JSON.parse. However, there is absolutely no reason to jump through these hoops, when you can simply write the JavaScript object as a normal JavaScript object literal.

另一个答案显示了一种定义常规JavaScript对象的方法,然后使用 JSON.stringify 将其转换为JSON 。但是,在您的情况下,除非您计划将对象发送到某处,否则无需执行此操作。

Another answer shows a way to define a regular JavaScript object, and then turn it into JSON using JSON.stringify. However, again, in your case, there is no need to do this unless you are planning to send the object somewhere.

另一个答案显示了定义常规的方法JavaScript对象,然后将其字符串化为JSON字符串,然后立即将其解析回JavaScript对象,该对象与字符串化和解析之前完全相同。绝对没有必要这样做。

Yet another answer shows a way to define a regular JavaScript object, then stringify it into a JSON string, then immediately parse it back into a JavaScript object, which will be exactly the same object you started with before stringifying and parsing. There's absolutely no need to do this.

在使用正则表达式时,人们会看到关于SO试图以某种方式操纵,搜索或替换JSON的问题。几乎没有任何情况你甚至需要考虑做这样的事情。 JSON有两个基本操作:将值转换为JSON,并将JSON转换为值。这些都是以 JSON.stringify JSON.parse 的形式构建到语言中的,可以完成他们的工作具有绝对的可靠性和性能。如果由于某种原因需要修改JSON字符串格式的数据,请不要操纵字符串;您需要做的就是将其解析为JavaScript值,操纵值,然后将其转换回JSON。

One sees no small number of questions on SO trying to somehow manipulate or search or replace within JSON, using regexps for example. There are almost no cases where you need to even think of doing such a thing. JSON has two fundamental operations: converting values into JSON, and converting JSON into values. Both of those are built into the language in the form of JSON.stringify and JSON.parse, that do their jobs with absolute reliability and performance. If for some reason you need to modify data which is in JSON string format, don't manipulate the string; all you need to do is parse it into a JavaScript value, manipulate the value, and then convert it back into JSON.

不幸的是,由于未知原因,许多JavaScript程序员正在学习这门语言,包括许多在SO上发布过问题的人,已经发现JSON 是一个JavaScript对象的错误认知,或者他们必须考虑JSON并进行字符串化和解析才能处理旧的JavaScript对象,非常的情况。

Unfortunately, for unknown reasons, many JavaScript programmers who are learning the language, including many who have posted questions here on SO, have developed the misperception that JSON is a JavaScript object, or that they must think about JSON and stringifying and parsing in order to deal with plain old JavaScript objects, which is very much not the case.

小点,但您在问题中使用术语解析。我看到越来越多的人出于某种原因使用这个词来表示访问或检索或提取。实际上,解析具有非常特定的含义:根据特定语法将基于字符串的信息表示(例如程序或数据对象)分析成某种适合的计算机友好形式以供进一步处理。什么 JSON.parse 在这个意义上解析 ,因为它正在根据JSON语法将基于字符串的信息表示分析为JavaScript进一步处理的对象。

Minor point, but you use the term "parse" in your question. I see more and more people using this word for some reason to mean "access" or "retrieve" or "extract". Actually, "parse" has a very specific meaning: to analyze a string-based representation of information (such as a program or data object), according to a particular grammar, into some suitable computer-friendly form for further processing. What JSON.parse does is parsing in this sense, because it is analyzing a string-based representation of information according to the JSON grammar into a JavaScript object for further processing.

这篇关于如何将变量解析为JSON对象(Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆