- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2019 05:20 AM
Hello experts,
I have created a HTML page & consumes custom application REST API. I need to parse the response which has JSON array & require to display the record id in the HTML page. I have tried to use it as JSON object but it did not help. I have attached the sample response in the thread.
Valuable feedback is required.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2019 05:04 AM
Thread closed. Issue has been fixed by the below code
var resp=this.response;
respo = JSON.parse(resp);
//console.log(respo.result.length);
for (var i=0; i < respo.result.length; i++)
{
console.log(respo.result[i].number);
alert(respo.result[i].number);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2019 11:49 PM
Already tried it but of no help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2019 05:42 AM
User JSONParser() to read the json string to object.
var json = new JSONParser();
var data = json.parse(responseBody);
var result = data["result"];
Do share the script if this does not help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2019 05:47 AM
This does not work.
Below is the JS to parse the response
function retrievelist()
{
var requestBody = "";
var client=new XMLHttpRequest();
client.open("get","https://dev22387.service-now.com/api/now/table/x_58872_needit_needit?sysparm_fields=number&sysparm_limit=2");
client.setRequestHeader('Accept','application/json');
client.setRequestHeader('Content-Type','application/json');
//Eg. UserName="admin", Password="admin" for this code sample.
client.setRequestHeader('Authorization', 'Basic '+btoa('admin'+':'+'******'));
client.onreadystatechange = function() {
if(this.readyState == this.DONE) {
//document.getElementById("response").innerHTML=this.status + this.response;
alert(this.response);
//var cc=this.response.toString();
// var json = new JSONParser();
var data = json.parse(this.response);
var result = data["result"];
alert(result);
}
};
client.send(requestBody);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2019 05:56 AM
Did you try by not commenting out the line
// var json = new JSONParser();
try JSON.parse(this.response) instead
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2019 11:40 PM
I tried it without commenting it.
Also, I have tried the below earlier & it was not of much help
JSON.parse(this.response)