4

I have a javascript object like follows.

{
    "name": {
        "type": "text",
        "onClick": function () {
            console.log("Hello");
        }
    }
}

It is stored in string format in Java like.

String obj = "{ \"name\": { \"type\": \"text\", \"onClick\": function () { console.log(\"Hello\"); } } }";

I'm trying to figure out a way to read this obj in Java and traverse through the object graph like we can with JSON using Jackson if it didn't have function declaration.

Is there any Java library to read/parse a string representing javascript object (not just JSON) and traverse through the object graph?

10
  • 1
    Who flagged to close this question, please specify reason. Commented Jan 8, 2016 at 5:41
  • @PritamBanerjee I looked at the documentation but couldn't figure out how Apache Wink could help read javascript object in java? Can you please reference to specific sections in documentation? Commented Jan 8, 2016 at 5:51
  • 1
    I was one of the flaggers (well, voted to close) because asking for library recommendations is off-topic on StackOverflow. Try Software Recommendations Commented Jan 8, 2016 at 5:54
  • That is not JSON. It can't be parsed using the standard libraries Commented Jan 8, 2016 at 5:56
  • @cricket_007 It is fine if there are any non-standard libraries to parse a string representing javascript object. Commented Jan 8, 2016 at 5:59

3 Answers 3

3

You could use Java's ScriptEngine and the Javascript built-in. Something like,

String obj = "{'name':{'type': 'text', 'onClick': function (){console.log('Hello')}}}";
try {
    ScriptEngine se = new ScriptEngineManager().getEngineByName("js");
    se.eval(String.format("Object.bindProperties(this, %s);", obj));
    se.eval("print(this.name.onClick)");
} catch (ScriptException e) {
    e.printStackTrace();
}

which can read the function declaration (and any of the other obj properties).

Sign up to request clarification or add additional context in comments.

Comments

1

You can use object mapper from jackson libarary to convert jsonString to hash map

import com.fasterxml.jackson.databind.ObjectMapper;

private Map<String, Object> getMapFromJson(String json){

    Map<String,Object> map = new HashMap<String,Object>();
    ObjectMapper mapper = new ObjectMapper();

    try {
        //convert JSON string to Map
        map = mapper.readValue(String.valueOf(json), new TypeReference<Map<String, Object>>() {} );
       return map;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

2 Comments

Jackson is working only for JSON. i.e without function declarations.
The point is if JSON string is just a key value pair. by this you will get your function as a value to the key 'onClick'. I.e a string. If you need a function to be parsed. you need to look for javascript parser implementation in java .
0

I suggest library org.json : JavaDoc URL, jar file Download

[Example]

String obj = "{ \"name\": { \"type\": \"text\", \"onClick\": function () { console.log(\"Hello\"); } } }";
JSONObject json = new JSONObject(obj);
JSONObject subJson = new JSONObject();

if( ! json.isNull("name") ){ //Determine if the value associated with the key("name") is null or if there is no value.
     subJson = json.getJSONObject("name");
     if( ! subJson.isNull("type") ){ // Determine if the value associated  with the key("type") is null or if there is no value.
        subJson.getString("type"); // get the value : "text"
        subJson.put("newData", "text2"); // data added under the "onclick"
    }
}

3 Comments

Not Working. org.json.JSONException: Expected a ',' or '}' at character 52
Could you replace your value like "function () { console.log(\"Hello\"); "?
"org.json" can read value only String or Number. if you want to put String value, use ""(Double Quote)

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.