1

I want to convert javascript multidimensional array to some java object. I have this array stored in string. Here is the example of array in my string

String array = "[1,2,3,[3,2,4],['asdasda','test','tsdsdf'],3,2,['sddd']]";

and structure can be in different this is just an example.

Is there any way to convert this array into some Java object?

3
  • 2
    you can use a json library like jackson or gson to do this Commented Jul 15, 2013 at 11:53
  • what kind of an array is this?? Commented Jul 15, 2013 at 11:54
  • simple JavaScript multidimensional array...stored as string.
    – Majky
    Commented Jul 15, 2013 at 11:57

2 Answers 2

2

Short answer: Yes. ;-)

Long answer: Considering how similar Java and JavaScript seem to be, this is surprisingly hard.

First of all, your input isn't in JSON format (for JSON, strings must use " quotes), so you can't use one of the many JSON parsing libraries like gson or jackson.

So the next option would be to use a JavaScript interpreter like Rhino, call evaluateString() and examine the result.

This gives you an JavaScript array instance which will feel very odd to use from a Java perspective - remember, JavaScript arrays are actually more like hash maps than plain Java arrays plus some JavaScript oddities.

If you want to get value 5 from such an array, you can use array.get(5, array);

Last option is to write your own parser.

1
  • Thank you for explanation. It was very helpful :) I'm trying javascript interpreter and it seems to be working. Thanks again!
    – Majky
    Commented Jul 16, 2013 at 6:27
0

To convert javascript to java object I use this function:

public <T> T toObject(String js, Class<T> classToReturn) throws ScriptException, JsonMappingException, JsonProcessingException {

    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
    scriptEngine.eval("variable = " + js);
    scriptEngine.eval("json= JSON.stringify(variable);");
    final String json =  (String) scriptEngine.get("json");

    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return mapper.readValue(json, classToReturn);
}

Example:

    final String js = "[{name:'FieldName', numbers: [1,2,3], strings: ['one','two','three'], innerClass:[{name:'InnerClass1', numbers: [4,5,6]},{name:'InnerClass2', strings: ['test']}]},  {strings: ['asdasda','tsdsdf']}]";
    final ClassToGet[] objects = toObject(js, ClassToGet[].class);

Class to get:

@Getter @Setter @ToString
public class ClassToGet{
    private String name;
    private int[] numbers;
    private List<String> strings;
    private Set<ClassToGet> innerClass;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.