ClientResource res =new ClientResource("/report.json"); //declare a restlet client resource
JsonRepresentation rep =new JsonRepresentation(response.getEntity());
// Displays the properties and values.
try {
JSONObject object = rep.getValue().isObject();
if (object !=null) {
for (String key : object.keySet()) {
System.out.println(key+":"+object.get(key));
//... use object.get(key).isXXX to retrive the value of key
}}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}JSON Type | Data Format | Java Type |
JSONArray | [{"name":"aaaaa","size":100},{"name":"bbb","size":200}] | List |
JSONObject | {"name":"bbb","size":200} | POJO Object |
JSONArray wrapped by JSONObject | {"reportList":[{"name":"aaaaa","size":100},{"name":"bbb","size":200}]} | Map(only one entry) |
complicated JSONObject | {"reportList":[{"name":"aaaaa","size":100},{"name":"bbb","size":200}], "report":{"name":"ccccc","size":0}} | Map |
public interface ReportJsonReader extends JsonReader<Report> {}
public interface ReportJsonWriter extends JsonWriter<Report> {}
ClientResource res =new ClientResource("/report.json"); //declare a restlet client resource
res.setOnResponse(new JsonModelsResponse<Report>(reportReader) {
@Override
public void onError(IOException error, Request request, Response response) {
//TODO: Handle error}
// models is deserialized list filled with POJO objects
}
res.setOnResponse(new JsonModelResponse<Report>(reportReader) {
@Override
public void onError(IOException error, Request request, Response response) {
//TODO: Handle error}
// model is deserialized POJO objects
}
<inheritsname="name.pehl.piriti.json.JSON"/>
<inheritsname="name.pehl.piriti.restlet.Restlet"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" ]]>
<propertyname="ignoreAcceptHeader"value="false"/>
<property name="defaultContentType"value="text/html"/>
<property name="mediaTypes" ]]>
<map ]]>
<entry key="atom"value="application/atom+xml"/>
<entry key="html"value="text/html"/>
<entry key="json"value="application/json"/>
</map ]]>
</property ]]>
<property name="viewResolvers" ]]>
<list ]]>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" ]]>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean ]]>
</list ]]>
</property ]]>
<property name="defaultViews" ]]>
<list ]]>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" ]]>
<!-- 默认即处理application/json请求 <property name="contentType" value="application/json"/> -->
</bean ]]>
</list ]]>
</property ]]>
</bean ]]><mvc:resources mapping="/**.html" location="/"/>
<mvc:resources mapping="/**.css" location="/"/>
<mvc:resources mapping="/**.ico" location="/"/>
<mvc:resources mapping="/gwt-module-name/**" location="/gwt-module-name/"/>