Question

I have an action class like

public class DataProcessor extends ActionSupport{

    private JSONObject object;

    public JSONObject getObject() {
        return object;
    }

    public void setObject(JSONObject object) {
        this.object = object;
    }

    @Override
    public String execute() throws Exception {
        .......
        return SUCCESS;
    }
} 

My XML mapping is like

<package name="default" extends="struts-default" namespace="/">
   <action name="process" class="com.demo.DataProcessor">
      <result type="success">home.jsp</result>
   </action>
</package>

on jsp page if i write <s:property value="object"/> it prints json data. bt if i write

<s:property value="object.name"/>

or

<s:property value="#object.name"/>

or

<s:property value="${#object.name}"/>  it is printing nothing.

How can i parse json object in jsp page?

Was it helpful?

Solution

You do not need to parse JSON you need to retrieve value from it. Assuming that your object is org.json.JSONObject you can get value from it by calling get method and passing key as string.

<s:property value="object.get('name')"/>

OTHER TIPS

You can parse JSON using the library function parseJSON like in this example

<s:textfield id="name" name="name"/>
<s:textfield id="mobile" name="mobile"/>    
<script type="text/javascript">
  $(document).ready(function() {
    var obj = $.parseJSON('<s:property value="object"/>');
    $("name").val(obj.name);
    $("mobile").val(obj.mobile);
  });
</script>

This script will replace the values (if any) from the action bean populated when JSP was rendered. The textfields should be mapped to the action bean correspondingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top