admin管理员组文章数量:1318028
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:
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.
My json could be something like:
{
"count": 2,
"e": 1,
"k": null,
"privateresult": 0,
"q": "surname",
"start": 0,
"cards": [
{
"__guid__": "efd192abc3063737b05a09a311df0ea0",
"pany": false,
"__title__": "name1 surname",
"__class__": "entity",
"services": false,
"__own__": false,
"vanity_urls": [
"name1"
]
},
{
"__guid__": "01917cfa71a23df0a67a4a122835aba8",
"photo": {
"__own__": false,
"__title__": null,
"__class__": "image",
"__guid__": "01917cfa71a23df04d03f83cb08c11e1"
},
"pany": 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 Is 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?
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;
}
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:
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.
My json could be something like:
{
"count": 2,
"e": 1,
"k": null,
"privateresult": 0,
"q": "surname",
"start": 0,
"cards": [
{
"__guid__": "efd192abc3063737b05a09a311df0ea0",
"pany": false,
"__title__": "name1 surname",
"__class__": "entity",
"services": false,
"__own__": false,
"vanity_urls": [
"name1"
]
},
{
"__guid__": "01917cfa71a23df0a67a4a122835aba8",
"photo": {
"__own__": false,
"__title__": null,
"__class__": "image",
"__guid__": "01917cfa71a23df04d03f83cb08c11e1"
},
"pany": 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 Is 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?
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;
}
Share
Improve this question
edited Jul 15, 2014 at 8:59
BenMorel
36.6k51 gold badges205 silver badges336 bronze badges
asked Jun 21, 2012 at 13:06
Gabriel ButoeruGabriel Butoeru
1311 gold badge3 silver badges14 bronze badges
3
- Possible dupe of: stackoverflow./questions/5618548/… – SomeKittens Commented Jun 21, 2012 at 13:11
- sorry if the way is not that pretty but I'm a beginner :D – Gabriel Butoeru Commented Jun 21, 2012 at 13:21
- Wele to Stackoverflow! Remember to upvote all useful answers, including those to others' questions. And choose/select the best answer to your own questions. – Larry K Commented Jun 21, 2012 at 13:22
2 Answers
Reset to default 2Your ining 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.
The json does include an array as the cards
element. -- Do want that?
You need to
- Parse the ining JSON into a JS structure. (Use JSON.parse.)
Once you have the structure in Javascript:
ining = JSON.parse(data); onionarray = ining.cards;
Also note that the modern way to declare an empty array is
var onionarray = [];
// not
var onionarray = new Array()
use the browser methods JSON.parse()
and JSON.stringify()
to convert your json to a javascript object and back.
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 = JSON.parse(data);
本文标签: Save a json in a javascript arrayStack Overflow
版权声明:本文标题:Save a json in a javascript array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742026333a2415611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论