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.
Related
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.
This is going to be a pretty ridiculous question so brace yourselves.
I need to build an URL string which will update some status variables in a website.
Basically, since the structure of the URL must be in JSON's structure, I'm having a lot of trouble getting variables in the middle of the string.
"http://mywebsite.com/index.php?data={\"Number\":\"1234567890001\",\"TS\":\"15/11/15%2008:08:31:44\",\"SER\":\"53543D303B44723D4E616F\"}"
Basically, I need to replace "1234567890001\" for the variable numberX and "53543D303B44723D4E616F\" for the variable serX.
Can someone lend me a hand?
I've tried everything with +'s & company but I'm not being able to pull it up.
Thanks!
Firstly, It's hard to understand why you are not using GET parameter. It will be a lot more easy to get all parameters
However, if you want to stay with this approach, you can simply get your data value and make a replace, like : replace("\\","")
This will provide a String similar to this : {"Number":"1234567890001","TS":"15/11/15%2008:08:31:44","SER":"53543D303B44723D4E616F"}
And then, you can simply parse it using JSON Library like GSON.
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.
Can someone please help me create a method to get the integers (listed below) from this link and store them in an array using Java? I'm new to Java and I've searched around and I can't any info or working examples of how to do it.
The integers are:
17,04,2011,1,2,7,10,13,23,24,25,26,27,38,39,41,43,45,48,49,55,59,62
All integers will change each time the method is called.
You need to do this:
retrieve the web page. Use URL and URLConnection for this.
Parse the HTML. It looks like this is an XHTML page, so you could use any XML API for this. As an alternative, for this simple structure something like a regular expression that throws away anything which is in <...> could work, too.
put the numbers from the HTML into a new array. You might want to count them first, to know the right size, or put them in while you are parsing, and later cut off the array. (Or they are always the same number.)
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.