简体   繁体   English

在JavaScript中将数组存储为JavaScript

[英]Storing arrays in javascript for javascript

I've seen lots of articles on how to serialise arrays from javascript to send to PHP files which the PHP file will then deserialise back into an array, etc... 我看过很多关于如何从javascript序列化数组以发送到PHP文件的文章,然后这些PHP文件将反序列化回数组,等等。

I have a large-ish nested associative array in my javascript which I need to save to a string and at a later date retrieve and convert back into a string in javascript . 我的javascript中有一个很大的嵌套关联数组,需要将其保存为字符串,稍后再检索并转换为javascript中的字符串。 No other program will ever touch this. 没有其他程序会碰到这个。 It may be handy if the serialised data is human-readable but it is absolutely not a requirement. 如果序列化数据是人类可读的,可能会很方便,但这绝对不是必需的。

There is an intermediate PHP file but it's only 4 lines long: all it does is save the string to a file and send it back when requested. 有一个中间PHP文件,但只有4行:它所做的只是将字符串保存到文件中,并在请求时发回。 I'm not interested in parsing the array in PHP. 我对解析PHP中的数组不感兴趣。

What is the fastest and simplest way to convert a multi-nested associative array in javascript to a string and back again? 将javascript中的多嵌套关联数组转换为字符串然后再次返回的最快,最简单的方法是什么?

JSON is a subset of the JavaScript language that is widely used as a serialization format, both for JavaScript and increasingly for other languages as well. JSON是JavaScript语言的子集,它广泛用作JavaScript的序列化格式,并且也越来越多地用于其他语言。 It's human-readable, except that most implementations remove optional whitespace, which makes it a bit more difficult. 它是人类可读的,除了大多数实现会删除可选的空格,这使它变得更加困难。

Modern browsers have JSON support out-of-the-box, for older ones you will need to include a library like json2.js . 现代浏览器具有开箱即用的JSON支持,对于较旧的浏览器,您将需要包含json2.js之类的库。

To serialize, you use the JSON.stringify method. 要序列化,请使用JSON.stringify方法。

var myObject = {a: 2, b: 3, c: [1, 2, 3]};
var serialized = JSON.stringify(myObject);
// serialized == "{"a":2,"b":3,"c":[1,2,3]}"

To unserialize, you use the JSON.parse method. 要反序列化,请使用JSON.parse方法。

var recovered = JSON.parse(serialized);

Well, I have constructed my array like: var data = new Array(); data["John"] = "John Smith"; 好吧,我的数组构造如下: var data = new Array(); data["John"] = "John Smith"; var data = new Array(); data["John"] = "John Smith";

Ah, this is a problem. 嗯,这是个问题。 A JavaScript Array isn't meant to be used as an associative array, it's meant as a normal zero-indexed non-associative array. JavaScript Array并不是要用作关联数组,而应是普通的零索引非关联数组。 You can assign properties (key/value pairs) to any JavaScript object, which is why your code is working, but the fact thay your object is an Array is probably going to cause problems. 您可以将属性(键/值对)分配给任何JavaScript对象,这就是为什么代码可以正常工作的原因,但是实际上您的对象是Array可能会引起问题。 If you just create a new Object() instead things should work better. 如果仅创建一个new Object()那么效果会更好。

You'll want to serialize your array into JSON: 您需要将数组序列化为JSON:

serialized = JSON.stringify(myobject)

To get it back 找回来

myobject = JSON.parse(serialized)

http://www.json.org/js.html http://www.json.org/js.html

var someArray = [];
someArray.push({someObjectProperty: 'someValue' });
someArray.push({someObjectProperty: 'someValue2' });

console.log(someArray);

var stringVersion = JSON.stringify(someArray);//You can save the string version anywhere...

console.log(stringVersion);

var someNewArray = JSON.parse(stringVersion);

console.log(someNewArray);

Everyone has explained this well on the Javascript side, but this is how it happens on the PHP side: 每个人都在Javascript方面对此进行了很好的解释,但这是在PHP方面的情况:

<?php
  $arr = Array(1, 2, 3, 'a', 'b', 'c');  // an array in PHP

  echo json_encode($arr); // output the array as JSON: [1,2,3,'a','b','c']
?>

<?php
  $json = "[1,2,3,'a','b','c']"; // a JSON string to be parsed by PHP

  $arr = json_decode($json); // this is our original array in PHP again
?>

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

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