从 stomp 消息响应 (JavaScript) 迭代 json 数组
Iterate through json array from stomp message response (JavaScript)
我收到来自 STOMP 订阅的消息正文响应,如下所示:
"[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]"
为了遍历它,我将它解析为 json 并得到:
[{"name":"test","version":"1.0.4","lifetime":25000}]
现在,当我尝试遍历它时,我收到以下错误消息:
Uncaught TypeError: Cannot use 'in' operator to search for '86' in [{"name":"test","version":"1.0.4","lifetime":25000}]
任何人都可以帮助我并指出我做错了什么吗?
这是我的代码:
stompClient.subscribe('/topic/validation', function(validationMessage){
console.log(validationMessage.body);
// --> "[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]"
var jsonObj = JSON.parse(validationMessage.body);
console.log(jsonObj);
// --> [{"name":"test","version":"1.0.4","lifetime":25000}]
$.each(jsonObj, function(i,item){
console.log(item);
});
});
提前致谢。
validationMessage 的控制台日志:
Frame{
ack: (headers)arguments: nullcaller: nulllength: 1name: ""prototype: frame.ack__proto__: ()<function scope>
body: ""[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]""
command: "MESSAGE"
headers: Object
content-length: "99"
content-type: "application/json;charset=UTF-8"
destination: "/topic/validation"
message-id: "1dm_aduc-1672"
subscription: "sub-0"__proto__: Objectnack: (headers)__proto__:
}
因为数值低于[]
。你需要经过两个 $.each
var jsonObj = [{"name":"test","version":"1.0.4","lifetime":25000}];
$.each(jsonObj, function(){
$.each(this, function(i,item){
$('body').append('<div>'+ item +'</div>');
});
});
var jsonObj2 = {"name":"test","version":"1.0.4","lifetime":25000}; // no []
$.each(jsonObj2 , function(i,item){
$('body').append('<div>'+ item +'</div>');
});
//////////////////////////////////////////////////////////////
var jsonObj3 = JSON.parse('[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]');
$.each(jsonObj3, function(){
$.each(this, function(i,item){
$('body').append('<div>'+ item +'</div>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
我收到来自 STOMP 订阅的消息正文响应,如下所示:
"[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]"
为了遍历它,我将它解析为 json 并得到:
[{"name":"test","version":"1.0.4","lifetime":25000}]
现在,当我尝试遍历它时,我收到以下错误消息:
Uncaught TypeError: Cannot use 'in' operator to search for '86' in [{"name":"test","version":"1.0.4","lifetime":25000}]
任何人都可以帮助我并指出我做错了什么吗?
这是我的代码:
stompClient.subscribe('/topic/validation', function(validationMessage){
console.log(validationMessage.body);
// --> "[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]"
var jsonObj = JSON.parse(validationMessage.body);
console.log(jsonObj);
// --> [{"name":"test","version":"1.0.4","lifetime":25000}]
$.each(jsonObj, function(i,item){
console.log(item);
});
});
提前致谢。
validationMessage 的控制台日志:
Frame{
ack: (headers)arguments: nullcaller: nulllength: 1name: ""prototype: frame.ack__proto__: ()<function scope>
body: ""[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]""
command: "MESSAGE"
headers: Object
content-length: "99"
content-type: "application/json;charset=UTF-8"
destination: "/topic/validation"
message-id: "1dm_aduc-1672"
subscription: "sub-0"__proto__: Objectnack: (headers)__proto__:
}
因为数值低于[]
。你需要经过两个 $.each
var jsonObj = [{"name":"test","version":"1.0.4","lifetime":25000}];
$.each(jsonObj, function(){
$.each(this, function(i,item){
$('body').append('<div>'+ item +'</div>');
});
});
var jsonObj2 = {"name":"test","version":"1.0.4","lifetime":25000}; // no []
$.each(jsonObj2 , function(i,item){
$('body').append('<div>'+ item +'</div>');
});
//////////////////////////////////////////////////////////////
var jsonObj3 = JSON.parse('[{\"name\":\"test\",\"version\":\"1.0.4\",\"lifetime\":25000}]');
$.each(jsonObj3, function(){
$.each(this, function(i,item){
$('body').append('<div>'+ item +'</div>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>