关于 JSON 的哈希到底是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2364289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:25:43  来源:igfitidea点击:

What exactly is a hash in regards to JSON?

jsonhashhashmap

提问by Alex

I am learning JSON, but I found out that you can put what are called "hashes" into JSON as well? Where can I find out what a hash is? Or could you explain to me what a hash is? Also, what's a hashmap? I have experience in C++ and C#, and I am learning JS, Jquery, and JSON.

我正在学习 JSON,但我发现您也可以将所谓的“哈希”放入 JSON 中?我在哪里可以找到哈希是什么?或者你能向我解释一下什么是哈希?另外,什么是哈希图?我有 C++ 和 C# 方面的经验,正在学习 JS、Jquery 和 JSON。

回答by Lior Cohen

A Hash is a sparse array that uses arbitrary strings/objects (depending on the implementation, this varies across programming languages) rather than plain integers as keys.

哈希是一个稀疏数组,它使用任意字符串/对象(取决于实现,这因编程语言而异)而不是普通整数作为键。

In Javascript, any Object is technically a hash (also referred to as a Dictionary, Associative-Array, etc).

在 Javascript 中,任何对象在技术上都是一个哈希(也称为字典、关联数组等)。

Examples:

例子:

  var myObj = {}; // Same as = new Object();
  myObj['foo'] = 'bar';

  var myArr = []; // Same as = new Array();
  myArr[0] = 'foo';
  myArr[1] = 'bar';
  myArr['blah'] = 'baz'; // This will work, but is not recommended.

Now, since JSON is basically using JS constructs and some strict guidelines to define portable data, the equivalent to myObj above would be:

现在,由于 JSON 基本上使用 JS 构造和一些严格的准则来定义可移植数据,因此与上面的 myObj 等效:

{ "foo" : "bar" };

Hope this helps.

希望这可以帮助。

回答by Instance Hunter

Hash = dictionary.

哈希 = 字典。

A hash:

一个哈希:

{ "key1": "value1", "key2": "value2" }

回答by Brice M. Dempsey

JSON supports dictionary type elements. People may refer to these as hash tables, which are a type of data structure. Referring to JSON dictionaries as hash tables would be technically incorrect, however, as there is no particular data structure implementation associated with the JSON data itself.

JSON 支持字典类型元素。人们可能将这些称为哈希表,它是一种数据结构。然而,将 JSON 字典称为哈希表在技术上是不正确的,因为没有与 JSON 数据本身相关的特定数据结构实现。

A hash is a random looking number which is generated from a piece of data and always the same for the same input. For example if you download files from some websites they will provide a hash of the data so you can verify your download is not corrupted (which would change the hash). Another application of hashes is in a hash table (or hash map). This is a very fast associative data structure where the hashes are used to index into an array. std::unorderd_map in C++ is an example of this. You could store a hash in JSON as a string for example something like "AB34F553" and use this to verify data.

散列是从一段数据生成的随机数,并且对于相同的输入始终相同。例如,如果您从某些网站下载文件,它们将提供数据的哈希值,以便您可以验证下载没有损坏(这会更改哈希值)。哈希的另一个应用是在哈希表(或哈希映射)中。这是一种非常快速的关联数据结构,其中散列用于索引数组。C++ 中的 std::unorderd_map 就是一个例子。您可以在 JSON 中将哈希存储为字符串,例如“AB34F553”之类的内容,并使用它来验证数据。