我正在将json从Java应用程序发送到节点js服务器,但是在服务器中,它仅显示密钥而不显示json的值。我得到正确的响应,但解析不正确。这是我将json发送到节点服务器和服务器代码时的Java代码
EXISTS
这是服务器代码
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setInstanceFollowRedirects(false);
httpURLConnection.connect();
OutputStreamWriter writer=new OutputStreamWriter(httpURLConnection.getOutputStream(),"UTF-8");
JSONObject student1 = new JSONObject();
JSONObject map = new JSONObject();
map.put("name", "abc");
writer.write(map.toString());
System.out.println(map.toString());
writer.close();
当我在Java中打印map.toString()时,它会显示正确的json
var express=require('express');
var app=express();
var mysql=require('mysql');
var bodyParser=require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(bodyParser.json());
app.post('/',urlencodedParser,function(req,res)
{
res.setHeader('Content-Type', 'application/json');
console.log(req.body);
res.end(JSON.stringify({ a: '1' }));
});
app.listen(8080,'127.0.0.1');
console.log('Now listening to port 8080');
,但在服务器中显示为
{"name","abc"}
如果有人找到解决方案,请回复!!
答案 0 :(得分:1)
请对此进行修改:
<p>Welcome to Forward Fitness Club. Our mission is to help our clients meet their fitness <b>AND</b> nutrition goals.<br> </p>
<h2> FREE ONE-WEEK TRIAL MEMBERSHIP</h2>
<a href="Contact Us.html">Call Us today to Get Started</a> <br>
<h3>Fitness Club Hours</h3> <br>
<p>
<ul>
<li> Monday-Thursday: 6:00am - 6:00pm</li>
<li> Friday: 6:00am - 4:00pm</li>
<li> Saturday: 8:00am - 6:00pm</li>
<li> Sunday: Closed</li>
</ul>
</p>
输出:
Map<String, String> map = new HashMap<>();
map.put("name", "abc");
JSONObject jsonObject = new JSONObject(map);
System.out.println(jsonObject);
答案 1 :(得分:0)
发现错误 添加
httpURLConnection.setRequestProperty("Content-Type", "application/json;
charset=UTF-8");