现在我正在尝试将Java List对象转换为JSON数组,并努力转换UTF-8字符串.我已经尝试了所有以下内容,但它们都不起作用.
设置.
response.setContentType("application/json");
PrintWriter out = response.getWriter();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
final ObjectMapper mapper = new ObjectMapper();
Run Code Online (Sandbox Code Playgroud)
测试#1.
// Using writeValueAsString
String json = ow.writeValueAsString(list2);
Run Code Online (Sandbox Code Playgroud)
测试#2.
// Using Bytes
final byte[] data = mapper.writeValueAsBytes(list2);
String json = new String(data, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
测试#3.
// Using ByteArrayOutputStream with new String()
final OutputStream os = new ByteArrayOutputStream();
mapper.writeValue(os, list2);
final byte[] data = ((ByteArrayOutputStream) os).toByteArray();
String json = new String(data, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
测试#4.
// Using ByteArrayOutputStream
final OutputStream os = new ByteArrayOutputStream();
mapper.writeValue(os, …Run Code Online (Sandbox Code Playgroud) 我需要添加当前JSON中不存在的属性.json对象如下所示.
var jsonObj = {
"result" : "OK",
"data" : []
};
Run Code Online (Sandbox Code Playgroud)
我想在'数据'中添加温度.我可以像下面这样做.
jsonObj.data.push( {temperature : {}} );
Run Code Online (Sandbox Code Playgroud)
然后,我想在'温度'内添加'home','work'.结果如下.
{
"result" : "OK",
"data" : [
{ "temperature" : {
"home" : 24,
"work" : 20 } }
]
};
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?我成功地在'数据'中插入'温度',但无法在温度范围内添加'home'和'work'.'温度'内可能有更多,所以需要用{}括起来.
我是Bootstrap的新手.但我对HTML很熟悉.问题是我无法从JSP获取参数值.参数来自带有Bootstrap 3的HTML页面.这是代码.我不明白.如果我只创建简单的html页面,那么我可以得到参数.
<form class="form-horizontal" method="POST" action="makeResv.jsp">
<div class="container">
<div class="row">
<div class="control-group col-md-5">
<label for="checkIn">date1</label>
<div id="date-container">
<div class="input-group date">
<input type="text" id="checkIn" class="form-control"><span
class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
</div>
<div class="control-group col-md-5">
<label for="checkOut">date2</label>
<div id="date-container">
<div class="input-group date">
<input type="text" id="checkOut" class="form-control"><span
class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-success">search</button>
</div>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
在JSP页面中,我只是执行以下操作.
String checkIn = request.getParameter("checkIn");
String checkOut = request.getParameter("checkOut");
out.println(checkIn + " " + checkOut);
Run Code Online (Sandbox Code Playgroud)
使用Bootstrap上面的HTML代码有什么问题?请给我一些建议.谢谢!!
顺便说一下,我正在使用datepicker JavaScript库,但我认为这不会影响结果.我不能在这里添加库代码,但无论如何它都很棒. https://github.com/eternicode/bootstrap-datepicker
json ×2
arrays ×1
attributes ×1
forms ×1
getparameter ×1
jackson ×1
javascript ×1
jsp ×1
list ×1
node.js ×1
utf-8 ×1