0

I've serialized an object from java into a json object in a file and now I want to use its data. But I don't know how to get this file which contain only my JSON object. I want to get it into an js object that I can use in a script.

mel
  • 2,730
  • 8
  • 35
  • 70
  • 1
    Do you mean `Java -> JSON -> Javascript` or `Java -> JSON -> Java` ? – ZombieSpy Jun 28 '15 at 19:57
  • I did Java -> json now I want to do JSON -> JavaScript – mel Jun 28 '15 at 20:01
  • 1
    For the parsing you can take a look at [this answer](http://stackoverflow.com/a/4935684/1938640) or [this one](http://stackoverflow.com/a/11550982/1938640) – ZombieSpy Jun 28 '15 at 20:05
  • 1
    And do you intend to serve the JSON to clients? What platform do you use on your server? – ZombieSpy Jun 28 '15 at 20:06
  • I just want to use its data in a highcharts chart but I need to get it into a variable. @ZombieSpy the part i'm interested in is to put the JSON Object that is in a file into a variable – mel Jun 28 '15 at 20:09
  • Which environment are you using - do you have web-server, Apache or similar, set up which could server the file for the JavaScript in browser, or are you planning to use it from node.js? – Tero Tolonen Jun 28 '15 at 20:28
  • I'm working on a localserver – mel Jun 28 '15 at 20:28
  • 1
    if on the localhost and for your own use one, JSON is JavaScript code so you might just create a HTML file and insert the JSON in and give that object to the chart – Tero Tolonen Jun 28 '15 at 20:30
  • 1
    @mel so you are planning on shipping your computer? :p – ZombieSpy Jun 28 '15 at 20:32
  • Not yet^^ just training – mel Jun 28 '15 at 20:34

1 Answers1

1

As answered here you can use jQuery's getJSON method.

$.getJSON("/path/to/my/json_file.json", function (json) {
    imaginaryFunctionThatDoesMagicStuff(json);
});

Or if you can grab the file in other ways you can use the javascript parse function:

JSON.parse(raw);

As suggested here


Example:

$.getJSON("http://json-stat.org/samples/order.json",function(data)
{
  document.write(data.order.label);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Community
  • 1
  • 1
ZombieSpy
  • 1,376
  • 12
  • 23