I have a jsp file that returns a json object using the following code:
我有一个jsp文件,使用以下代码返回一个json对象:
JSONObject object = new JSONObject();
object.put("name","domain");
object.put("email","domain.com");
response.setContentType("application/json");
response.getWriter().write(object.toString());
The output is: {"name":"domain","email":"domain.com"}
输出为:{“name”:“domain”,“email”:“domain.com”}
I trying to get the values from this JSON using following code in node.js:
我尝试使用node.js中的以下代码从此JSON获取值:
var endpoint = // contains the address of the above jsp file;
var body = ""
http.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
console.log("Body: "+body);
console.log("Body name: "+body.name);
})
})
In the above snippet I get following output for console.log -
在上面的代码片段中,我得到了console.log的以下输出 -
Body: {"name":"domain","email":"domain.com"}
Body name: undefined
正文名称:未定义
I don't know why "body.name" is not working. Could any body please help in getting the values from the json object. Since, body itself is json object so I don't need to do JSON.parse
我不知道为什么“body.name”不起作用。任何正文可以帮助从json对象获取值。因为,body本身就是json对象,所以我不需要做JSON.parse
2 个解决方案
#1
0
Try this. You have to parse the JSON string to assign it to a js object.
尝试这个。您必须解析JSON字符串以将其分配给js对象。
var endpoint = // contains the address of the above jsp file;
var body = {}
http.get(endpoint, (response) => {
response.on('data', (chunk) => { body = JSON.parse(chunk) })
response.on('end', () => {
console.log("Body: "+body);
console.log("Body name: "+body.name);
})
})
#2
1
body
object is string. Because of that when you try to write it in console:
body对象是字符串。因此,当您尝试在控制台中编写它时:
console.log("Body: "+body);
You get this:
你得到这个:
Body: {"name":"domain","email":"domain.com"}
But since body is string you cannot get its property name
. String doesn't have proerty name
. You should firstly parse string to JSON
但由于body是字符串,因此无法获取其属性名称。 String没有proerty名称。您应该首先将字符串解析为JSON
var endpoint = // contains the address of the above jsp file;
var body = ""
http.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
console.log("Body: "+ body);
var parsedBody = JSON.parse(body);
console.log("Body name: "+ parsedBody .name);
})
})
#1
0
Try this. You have to parse the JSON string to assign it to a js object.
尝试这个。您必须解析JSON字符串以将其分配给js对象。
var endpoint = // contains the address of the above jsp file;
var body = {}
http.get(endpoint, (response) => {
response.on('data', (chunk) => { body = JSON.parse(chunk) })
response.on('end', () => {
console.log("Body: "+body);
console.log("Body name: "+body.name);
})
})
#2
1
body
object is string. Because of that when you try to write it in console:
body对象是字符串。因此,当您尝试在控制台中编写它时:
console.log("Body: "+body);
You get this:
你得到这个:
Body: {"name":"domain","email":"domain.com"}
But since body is string you cannot get its property name
. String doesn't have proerty name
. You should firstly parse string to JSON
但由于body是字符串,因此无法获取其属性名称。 String没有proerty名称。您应该首先将字符串解析为JSON
var endpoint = // contains the address of the above jsp file;
var body = ""
http.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
console.log("Body: "+ body);
var parsedBody = JSON.parse(body);
console.log("Body name: "+ parsedBody .name);
})
})