0

I am at a point where I can pull a single javascript declaration such as:

var cars = ["Saab", "Volvo", "BMW"];

parsed from a page.

I would like to be able to get all the elements of the array ("Saab", "Volvo", "BMW") from this declaration.

Should I be using some javascript engine for this, or what else would be the best way to get javascript variable values from my Java code.

I would hate to reinvent the wheel if something is already out there that is able to do this, so I am just looking for advice on something I can use to do this function.

blgrnboy
  • 4,877
  • 10
  • 43
  • 94
  • Possible duplicate of http://stackoverflow.com/questions/7487908/how-can-i-use-javascript-in-java ? – Aurelien Dec 04 '15 at 10:07

4 Answers4

1

I assume you found a way to transport that javascript object/array into your Java domain as a String or Stream. What you want now is a JSON parser.

One way is to use json.org or other libraries. Further information about json parsing can be found in this thread: How to parse JSON in Java

The [org.json][1] library is easy to use. Example code below:

import org.json.*;


JSONObject obj = new JSONObject(" .... ");
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
} You may find extra examples from: [Parse JSON in Java][2]

Downloadable jar: http://mvnrepository.com/artifact/org.json/json

[1]: http://www.json.org/java/index.html
[2]: http://theoryapp.com/parse-json-in-java/

You might also want to look into jsonb (https://jcp.org/en/jsr/detail?id=353) that was introduced with Java 7. You can bind an object model and transform JSON objects into java objects and vice versa.

Community
  • 1
  • 1
Alex
  • 1,141
  • 1
  • 13
  • 24
0

you can iterate through all the values in 'window'

for ( var key in window )
{
   if ( typeof window]key] == 'object' && window]key].length > 0 )
   {
      //this is the array you are looking for
   }
}

You can get access to javascript object from java by using httpunit

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • But remember, I am trying to do this from Java, not JavaScript. Therefore, I am looking for either a good JavaScript parser for Java, or a method I can define myself that can get this. Ideally, I would be able to retrieve not just arrays, but any type of javascript object. – blgrnboy Dec 04 '15 at 10:02
  • @blgrnboy try httpunit http://httpunit.sourceforge.net/doc/javascript-support.html – gurvinder372 Dec 04 '15 at 10:06
0

With JDK 8 the code bellow works :

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

    String js = "var carsfromjs = [\"Saab\", \"Volvo\", \"BMW\"]";

    engine.eval(js);

    String[] cars = (String[])engine.eval("Java.to(carsfromjs, \"java.lang.String[]\")");

    for(int i=0; i<cars.length; i++){

        System.out.println(cars[i]);
    }

You can find many ways to access Javascript code throught "nashorn" :

  1. http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/
  2. http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html
  3. http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/
Aurelien
  • 458
  • 1
  • 5
  • 13
0

Method 1: JSON parser, as Alex's answer.

Method 2: Javascript parser for Java

Method 3: Regular Expression (A weird way I figured out!)

First pattern is var\s+([a-zA-Z0-9]+)\s+=\s+\[(.*)\]\s*;*
var + one or more space(s) + variable name($1) + one or more space(s) + equals sign + one or more space(s) + array content($2) + ......

Second pattern is "(.*?)", get the string between two quotation marks.

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JSParser {
    public String arrayName;
    private String tempValues;
    public ArrayList<String> values = new ArrayList<String>();

    public boolean parseJSArray(String arrayStr){

        String p1 = "var\\s+([a-zA-Z0-9]+)\\s+=\\s+\\[(.*)\\]\\s*;*";
        Pattern pattern1  = Pattern.compile(p1);
        Matcher matcher = pattern1.matcher(arrayStr);
        if(matcher.find()){
            arrayName = matcher.group(1);
            tempValues = matcher.group(2);

            Pattern getVal  = Pattern.compile("\"(.*?)\"");
            Matcher valMatcher = getVal.matcher(tempValues);
            while (valMatcher.find()) { // find next match
                String value = valMatcher.group(1);
                values.add(value);
            }
            return true;
        }else{
            return false;
        }

    }

}
Community
  • 1
  • 1
Jean Y.C. Yang
  • 4,382
  • 4
  • 18
  • 27