admin管理员组

文章数量:1394066

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?

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?

Share Improve this question edited Jan 9, 2016 at 5:44 TheKojuEffect asked Jan 8, 2016 at 5:31 TheKojuEffectTheKojuEffect 21.1k20 gold badges93 silver badges127 bronze badges 10
  • 1 Who flagged to close this question, please specify reason. – TheKojuEffect 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? – TheKojuEffect Commented Jan 8, 2016 at 5:51
  • 1 I was one of the flaggers (well, voted to close) because asking for library remendations is off-topic on StackOverflow. Try Software Remendations – Vince Commented Jan 8, 2016 at 5:54
  • That is not JSON. It can't be parsed using the standard libraries – OneCricketeer 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. – TheKojuEffect Commented Jan 8, 2016 at 5:59
 |  Show 5 more ments

3 Answers 3

Reset to default 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).

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

import .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;
    }
}

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"
    }
}

本文标签: Read javascript object in javaStack Overflow