我正在mobilefirst中做一个soap wsdl调用应用程序。我从soap wsdl中获取json形式的响应。响应存储在结果变量中。我正在使用result.length 但它显示长度未定义错误。在浏览了很多 stackoverflow 后,我发现 json 响应不是 array [] 它是 {}。我使用了 Object.keys() 并打印了我的 json 值的长度(名称、电子邮件等),现在我需要打印所有单独的值,请帮助如何做到这一点
{
"Envelope":{
"Body":{
"processResponse":{
"JOB":"Manager",
"NAME":"Rahul Sashanka",
"PHOTO":"\/9j\/4AAQSkZJRgABAQAAAQABAAD"
"POSITION":"Sales Support Manager",
"SUPERVISOR_NAME":"Ashraf Tarabulsy",
"USER_ID":"44",
"client":"http:\/\/xmlns.oracle.com\/InternetMobile\/AbsManagement\/BPELProcessUserProfile",
"xmlns":"http:\/\/xmlns.oracle.com\/InternetMobile\/AbsManagement\/BPELProcessUserProfile"
}
},
"Header":{
"FaultTo":{
"Address":"http:\/\/www.w3.org\/2005\/08\/addressing\/anonymous"
},
"MessageID":"urn:D9785BB07F9011E5BF8B25E60F40847D",
"ReplyTo":{
"Address":"http:\/\/www.w3.org\/2005\/08\/addressing\/anonymous"
}
},
"env":"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/",
"wsa":"http:\/\/www.w3.org\/2005\/08\/addressing"
},
"errors":[
],
"info":[
],
"isSuccessful":true,
"responseHeaders":{
"Content-Length":"8207",
"Content-Type":"text\/xml; charset=utf-8",
"Date":"Sat, 31 Oct 2015 05:33:00 GMT",
"SOAPAction":"\"\"",
"X-ORACLE-DMS-ECID":"9e10a9dcf92c80fa:38c0ef04:150b8318e90:-8000-0000000000022100",
"X-Powered-By":"Servlet\/2.5 JSP\/2.1"
},
"responseTime":163,
"statusCode":200,
"statusReason":"OK",
"totalTime":216,
"warnings":[
]
}
function displayFeeds(result){
var ul = $('#page1display');
for (var i= 0; i< Object.keys(result).length; i++) {
alert(Object.keys(result).length);//displays 8 as it contain 8 elements in result
var li = $('<li/>').html("NAME" +(Object.keys(result[i])).NAME);
//li.append($('<li/>').html("PERSONNAME:" +));
li.append($('<hr>'));
ul.append(li);
}
}
<script>
$.getScript(path + "js/Page1.js");
</script>
<p id="currentPage"> </p>
<input type="button" id="hr" class="appButton" value="HR" onclick="funchr();" />
<ul id="page1display"> </ul>
最佳答案
首先,您的回复在第七行出现一个错误,后面没有逗号
"PHOTO":"\/9j\/4AAQSkZJRgABAQAAAQABAAD"
这只是单个响应,因此它只打印单个名称七次。您可以根据自己的情况进行更改。
<<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
$(document).ready(function(){
var jsonresponse = {
"Envelope":{
"Body":{
"processResponse":{
"JOB":"Manager",
"NAME":"Rahul Sashanka",
"PHOTO":"\/9j\/4AAQSkZJRgABAQAAAQABAAD",
"POSITION":"Sales Support Manager",
"SUPERVISOR_NAME":"Ashraf Tarabulsy",
"USER_ID":"44",
"client":"http:\/\/xmlns.oracle.com\/InternetMobile\/AbsManagement\/BPELProcessUserProfile",
"xmlns":"http:\/\/xmlns.oracle.com\/InternetMobile\/AbsManagement\/BPELProcessUserProfile"
}
},
"Header":{
"FaultTo":{
"Address":"http:\/\/www.w3.org\/2005\/08\/addressing\/anonymous"
},
"MessageID":"urn:D9785BB07F9011E5BF8B25E60F40847D",
"ReplyTo":{
"Address":"http:\/\/www.w3.org\/2005\/08\/addressing\/anonymous"
}
},
"env":"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/",
"wsa":"http:\/\/www.w3.org\/2005\/08\/addressing"
},
"errors":[
],
"info":[
],
"isSuccessful":true,
"responseHeaders":{
"Content-Length":"8207",
"Content-Type":"text\/xml; charset=utf-8",
"Date":"Sat, 31 Oct 2015 05:33:00 GMT",
"SOAPAction":"\"\"",
"X-ORACLE-DMS-ECID":"9e10a9dcf92c80fa:38c0ef04:150b8318e90:-8000-0000000000022100",
"X-Powered-By":"Servlet\/2.5 JSP\/2.1"
},
"responseTime":163,
"statusCode":200,
"statusReason":"OK",
"totalTime":216,
"warnings":[
]
};
for (var i= 0; i< Object.keys(jsonresponse).length; i++) {
console.log(jsonresponse.Envelope.Body.processResponse.NAME);
};
})
</script>
</html>
关于javascript - 使用js在html中显示json响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33449141/