Provide data to highcharts from a Java method - java

I have a java method which retrieves data from database and a bean which manages this data.
Using Arrays.toString(var), I get this array [{'tom','1'},{'sawyer','2'}] but highcharts accept data only in this format ['tom','1','sawyer','2']
For now I have to get the array and using replace function to get the correct format but all this has to be done manually.
My question is how to convert the array to the correct format and then pass it to the highcharts data.
How to load a Java method on page load?
Thank you for your patience and help in advance.

Data that needs to be passed to highcharts must be JSON. So the best way to do this is to use a JSON library. http://json.org/java/

Data can be passed also using xml.. The best way to pass data i found is to create a string tn the java class by itself and in the page calling this java method we just need to create an xml dom parser which will convert the string to xml and thus push data to highcharts using jQuery easily.

Related

Validate JSON before further processing

I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.

is it possible to use a java array in javascript function?

Im trying to pass an array of strings from a java servlet to a jsp file.
now, i want to use that array in a javascript function in that same jsp file.
is this possible?
You'd have to first serialize it (JSON format is probably easiest) and then deserialize it in javascript to be able to use it as an array.
Yes, you'll need to convert the array to JSON first though, then have your JavaScript function parse that JSON. At that point it can work with it as a standard JS array (because at that point, it is).
EDIT - I did not mean to imply that JSON is absolutely required, nor that it is the only possible solution. It's simply an option, and in my opinion probably the best and easiest for what you're trying to accomplish.

Java based JSON manipulations

Is anyone aware of a JSON-XPath style library that allows data manipulation; update, delete, create, etc...
JsonPath.write(json, "$.store.book[*].author", value);
I've looked into the following, but none allow altering the content.
JPath
JSONQuery
JSONiJ
JsonPath (im using 2.2.0) now allows the manipulation of JSON data. e.g.
String jsonData = "{\"drink\":\"juice\"}";
JsonPath.parse(jsonData).set("$.drink", "beer").jsonString();
results in {"drink":"beer"}
JSON was not meant to be a database.
If you want to store your data in the JSON format;
Read the JSON records into your Java application and create data objects.
Modify the data objects in the Java application.
When the application closes, write the JSON records back out.
You'd be better off using an actual database, relational or NoSQL, to store your data, and write JSON records when they're needed.
You might want to have a look at this library I developped to be able to use XML libraries to manipulate JSON: https://github.com/bhabegger/json-n-xml/
It parses JSON to a DOM structure which you can manipulate with standard XML tools and then allows you to serialize back to json.
(JSON may not be meant to be a database but you do have occasions where you just want simple modifications.)
Hope it helps.

How to send hashmap with a file in GWT

I want to know or some code sample which will help me to send the file and the hashmap to the server.
to upload a file to GWt server i use formpanel and a HttpServlet.
this is working fine.
i have a hashmap
private static Map<String, List<Customproperties>> docClass =
new HashMap<String, List<Customproperties>>();
which holds the property of document according to its classname.
I know how to do with RPC. but i want to do with servlet.As i have to upload a file which i have done with servlet. And every Hashmap is related to file .and this file with its property(in HashMap) will send to external repository.
Please help.
There are 2 ways to convert a hashmap to a string (and convert it back to a hashmap)
1: Convert it using JSON library http://json-lib.sourceforge.net/ This will allow you to convert any java object to a JSON string so you can transfer it anywhere. And using the same library or another JSON library, can convert it back to a Java object.
2: Convert it to an XML string using a library called XStream http://x-stream.github.io/ This will convert any Java object into a String represented as XML.
I would recommend to convert your Objects to JSON strings because you are using GWT and it has a lot of support for JSON. And JSON is a good format for Webapps. Another advantage is that other languages can convert your JSON string into an object too.

Send ArrayList over the network - Convert Java ArrayList to XML String

I'm new to Java. I want to send an array (ArrayList) of objects over the network via Java Web Service to my Silverlight app. This ArrayList contains custom class objects:
ArrayList<SVNSearchResult> results
so I'm thinking the best way is to serialize this to an XML String and on the Silverlight part, use LinQ to parse it. If there's a better way to send it please let me know. Thanks.
XML is a good fit for this. JSON would be one of the other usual suspects these days.
Whatever format you end up choosing, make sure you get the encoding right.
For a starter, try JSON. It has a network-efficient format, and is supported by any major language in the world.
XML is only my second choice as it is more complicated to generate/parse and is more verbose.

Categories

Resources