简体   繁体   English

JSON解析返回未定义

[英]JSON parse returns undefined

I'm receiving a STOMP message with some JSON data, the problem is that I one case the code that I'm using works 我收到带有一些JSON数据的STOMP消息,问题是我有一种情况我正在使用的代码有效

function connect() {
    var socket = new SockJS('/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings', function (greeting) {
            console.log("data -->" +greeting + " <-- data");
            console.log(JSON.parse(greeting.body) + " <-- JSON object");
            console.log(JSON.parse(greeting.body).content + " <-- parsed data");
            showGreeting(JSON.parse(greeting.body).content);
        });
    });
}

the result 结果

data -->MESSAGE
content-length:34
message-id:4wb1eunw-1
subscription:sub-0
content-type:application/json;charset=UTF-8
destination:/topic/greetings
content-length:34

{"content":"user has subscribed!"} <-- data
[object Object] <-- JSON object
user has subscribed! <-- parsed data

And I'm using almost the same code in another project 我在另一个项目中使用几乎相同的代码

function connect() {
    var socket = new SockJS('/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/golden/notification', function (notification) {
            console.log("data -->" +notification + " <-- data");
            console.log(JSON.parse(notification.body) + " <-- JSON object");
            console.log(JSON.parse(notification.body).content + " <-- parsed data");
            showNotification(JSON.parse(notification.body).content);
        });
    });
}

But the result is somehow different 但是结果有所不同

data -->MESSAGE
content-length:39
message-id:wh4z1ahx-0
subscription:sub-0
content-type:application/json;charset=UTF-8
destination:/golden/notification
content-length:39

{"notification": "user has subscribed!"} <-- data
[object Object] <-- JSON object
undefined <-- parsed data

I'm a bit new to Js so the answer may be really simple but i can't findany solutions to solve this problem. 我对Js有点陌生,所以答案可能真的很简单,但我找不到解决此问题的任何解决方案。

PS this script is part of a Sring Boot application and the librarys are the same. PS此脚本是Sring Boot应用程序的一部分,并且库是相同的。

So thanks to @CertainPerformance. 因此,感谢@CertainPerformance。 As you see in the first example my JSON object had an element "content" 如您在第一个示例中看到的那样,我的JSON对象具有一个元素“ content”

{"content":"user has subscribed!"}

And it got its value by using JSON.parse(notification.body).content 它通过使用JSON.parse(notification.body).content来获得其价值

But in the second example, there was no element "content" only "notification" 但是在第二个示例中,没有元素“内容”只有“通知”

{"notification": "user has subscribed!"}

So to get the value of "notification" you need to use JSON.parse(notification.body).notification 因此,要获取“ notification”的值,您需要使用JSON.parse(notification.body).notification

I hope that this post will be helpful for someone. 我希望这篇文章对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM