如何在网页上格式化JSONObject

时间:2014-04-07 13:27:36

标签: json jsp format

我在java文件中创建了一个JSONOject

public String handleGet() {
try {
JSONObject system = new JSONObject();

system.put("System", "Disarmed");
system.put("Front Door", "Unlocked");
system.put("Back Door", "Unlocked");
system.put("Window 1", "Unlocked");
system.put("Window 2", "Unlocked");
system.put("Window 3", "Unlocked");
system.put("Lights", "On");

JsonRepresentation jsonRep = new JsonRepresentation(system);

return jsonRep.getText();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}

并且网页上的最终结果如下所示,全部在一行中;

{"Back Door":"Unlocked","Window 2":"Unlocked","Window 3":"Unlocked","Window 1":"Unlocked","Front Door":"Unlocked","System":"Disarmed","Lights":"On"}

http://finalyearporject.appspot.com/myfirstrestserver

有没有办法格式化这些数据,以便在列表,子弹或表格格式中显示它。以便最终用户可以阅读?或链接到jsp页面?

1 个答案:

答案 0 :(得分:0)

您可以使用Google提供的gson。它具有漂亮的打印功能,用于用户友好的JSON格式。 的 GSON