将 json 保存在 javascript 数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11138845/
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
Save a json in a javascript array
提问by Gabriel Butoeru
I'm trying to save a json in a javascript array but I don't understand how to do it. I don't want to save all the content in the first element of the array. That's my function:
我正在尝试将 json 保存在 javascript 数组中,但我不明白该怎么做。我不想将所有内容保存在数组的第一个元素中。这就是我的功能:
function jeyson()
{
var onion = new XMLHttpRequest();
onion.open("GET", "json.json", true);
onion.send(null);
/* var anotheronion = angular.fromJson(onion.responseText, false); */
var onionarray = new Array()
}
onionarray is the array that I'd like to contain my json file.
onionarray 是我想要包含我的 json 文件的数组。
My json could be something like:
我的 json 可能是这样的:
{
"count": 2,
"e": 1,
"k": null,
"privateresult": 0,
"q": "surname",
"start": 0,
"cards": [
{
"__guid__": "efd192abc3063737b05a09a311df0ea0",
"company": false,
"__title__": "name1 surname",
"__class__": "entity",
"services": false,
"__own__": false,
"vanity_urls": [
"name1"
]
},
{
"__guid__": "01917cfa71a23df0a67a4a122835aba8",
"photo": {
"__own__": false,
"__title__": null,
"__class__": "image",
"__guid__": "01917cfa71a23df04d03f83cb08c11e1"
},
"company": false,
"__title__": "name2 surname",
"__class__": "entity",
"services": false,
"__own__": false,
"vanity_urls": [
"name2"
]
}
]
}
How can I save it in my array?
如何将它保存在我的数组中?
PS I don't have problems like "same origin policy" because the json file is on the same server (for now I'm working with local files. Could I work with files that are not on the same domain?).
PS 我没有像“同源策略”这样的问题,因为 json 文件在同一台服务器上(现在我正在处理本地文件。我可以处理不在同一域中的文件吗?)。
PSIs it possible to use json as a javascript object (like an array)? If I want to search something in my json object how can I do it?
PS是否可以将json用作javascript对象(如数组)?如果我想在我的 json 对象中搜索一些东西,我该怎么做?
function geisson()
{
var iabile = new XMLHttpRequest();
iabile.open("GET", "json.json", true);
iabile.send(null);
var objectjson = {};
objectson = JSON.parse(iabile.responseText);
alert(objectson.cards.toSource());
return false;
}
回答by Christoph
use the browser methods JSON.parse()
and JSON.stringify()
to convert your json to a javascript object and back.
使用浏览器的方法JSON.parse()
和JSON.stringify()
你的JSON转换为JavaScript对象和背部。
In your case, onionarray
should not be an array but rather an object (In fact arrays and objects are >pretty< similar in javascript). You can assign your jsondata like that:
在你的情况下,onionarray
不应该是一个数组,而是一个对象(实际上数组和对象在 javascript 中 >pretty< 类似)。您可以像这样分配 jsondata:
onionarray = JSON.parse(data);
回答by Larry K
Your incoming JSON is not an array; it is a JSON structure (in Javascript, the same as an object). So it can not be directly stored as an array.
您传入的 JSON 不是数组;它是一个 JSON 结构(在 Javascript 中,与对象相同)。所以不能直接存储为数组。
The json does include an array as the cards
element. -- Do want that?
json 确实包含一个数组作为cards
元素。——要吗?
You need to
你需要
- Parse the incoming JSON into a JS structure. (Use JSON.parse.)
Once you have the structure in Javascript:
incoming = JSON.parse(data); onionarray = incoming.cards;
- 将传入的 JSON 解析为 JS 结构。(使用 JSON.parse。)
在 Javascript 中获得结构后:
incoming = JSON.parse(data); onionarray = incoming.cards;
Also note that the modern way to declare an empty array is
另请注意,声明空数组的现代方法是
var onionarray = [];
// not
var onionarray = new Array()