admin管理员组文章数量:1426783
I am working on Dynamic Web Project in Eclipse and using HTML 5 and Javascript as well. I could manage to parse JSON file that is saved locally in my system though the code below:
$.getJSON( "/Users/Documents/workspace2/sample.json", function( data ) {
var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
Let's suppose this is the JSON file:
{"resource":"A","literals":["B","C","D"]}
My question is: Is there any possibility to have an array of strings to store these elements that are inside the JSON file after parsing. I am quite new to jQuery and couldn't really manage to see these elements stored in an array of strings, for example. Could anyone help me with this issue please.
I am working on Dynamic Web Project in Eclipse and using HTML 5 and Javascript as well. I could manage to parse JSON file that is saved locally in my system though the code below:
$.getJSON( "/Users/Documents/workspace2/sample.json", function( data ) {
var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
Let's suppose this is the JSON file:
{"resource":"A","literals":["B","C","D"]}
My question is: Is there any possibility to have an array of strings to store these elements that are inside the JSON file after parsing. I am quite new to jQuery and couldn't really manage to see these elements stored in an array of strings, for example. Could anyone help me with this issue please.
Share Improve this question edited Oct 31, 2013 at 17:13 Blazemonger 93.1k27 gold badges145 silver badges181 bronze badges asked Oct 31, 2013 at 17:12 ModMod 5,3617 gold badges30 silver badges49 bronze badges 5- 6 It's not clear to me what you want to do. Can you show us what the desired result of your code should be? – Blazemonger Commented Oct 31, 2013 at 17:14
- Well. I am using Arobr javascript for visualisation. At some point, I should show nodes that are labeled with strings. I would like to get from JSON file "A", "B", "C", and "D", store them in an array of string and use them for the Arbor Javascript to get them visualised in nodes. After parsing, what can I do to read or store "A", "B", "C" and "D" as strings in order to be used for later. – Mod Commented Oct 31, 2013 at 17:19
-
you could save
data
, which is an object at this point, to a variable that is scoped outside of thegetJSON
call but realizegetJson
is an asynchronous operation. So when this runs other things will continue to run until this pletes then this will run the callback (the anon function you define in the call), so any variable you set inside of thegetJson
callback may not be available when you need it. – scrappedcola Commented Oct 31, 2013 at 17:19 - I would avoid storing presentation detail (i.e. the HTML) in your model, as really you should have separation of concerns i.e. It should be your presentation layer's responsibility to render the view, the data (i.e. your JSON) should be view/presentation agnostic i.e. have no knowledge of presentation detail. – Ben Smith Commented Oct 31, 2013 at 17:22
- I have defined an array which is outside the scope of getJson and added the following line: {outer.push( "<li id='" + key + "'>" + val + "</li>" ); After that I added the following line to see the first element of the array: {var node = sys.addNode('The First Element',{'color':'red','shape':'dot','label':outer.get(0)});} Unfortunately I cant get the first element of the string however it is defined outside the scope of getJson. Any idea, please? – Mod Commented Oct 31, 2013 at 17:32
2 Answers
Reset to default 2Try this way :
jsonFile.json
{
"resource":"A",
"literals":["B","C","D"]
}
myFile.html
<html>
<head>
<script src="http://code.jquery./jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$.getJSON( "jsonFile.json", function( data ) {
var items = [];
$.each( data, function( key, val1 ) {
items.push( "<li><a href=#'" + key + "'>" + val1 +"</a></li>");
});
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});
</script>
</head>
<body>
//result will be displayed here
</body>
</html>
Output :
var abc = JSON.stringify({"resource":"A","literals":["B","C","D"]});
var ab = JSON.parse(abc);
var arr=$.map(ab, function(value, index) {
return [value];
});
Hope it helps.
本文标签: javascriptHow to store JSON data from a file into an array of stringsStack Overflow
版权声明:本文标题:javascript - How to store JSON data from a file into an array of strings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745485916a2660382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论