0

So, i have a script that gets JSON from a server and displays it to a page, the JSON has html in it which has to be displayed, but for some reason when i try to convert the extracted JSON(which has the html div i want) to a string the contents are lost and i get an empty object.

i want to convert it to a string so innerHTML can insert it to the page

iv'e tried to convert it using String(obj) and obj.toString() and JSON.stringify all of them return an empty object

event just trying to insert the data without turning it into a string displays an empty object on the page

here's the json:

{
  "content": {
    "rendered": "<div id=aboutnf><p>lorem ipsum</p></div>",
    "protected": false
  }
}

and here's the javascript:

function displayContents() {
  //if the request succeeds display the contents
  if (httpRequest.readyState === XMLHttpRequest.DONE) {
    if (httpRequest.status === 200) {
      //parse the JSON
      var JsonObj = JSON.parse(httpRequest.responseText);//parse the response
      var content = JsonObj.content;
      console.log(content);
      var StrContent = JSON.stringify(content);
      //parse the html from the json
      var parser = new DOMParser();
      var stuff = parser.parseFromString(StrContent, "text/html");
      //get the div we want
      var disp = stuff.getElementById(aboutnf);
      console.log(disp);
      console.log(typeof(disp));
      //convert it to a string
      var dispContents = String(disp);
      console.log(dispContents);
      //insert it to the page
      document.getElementById('stuff').innerHTML = dispContents;
      //if the request failed report it
    } else {
      alert('There was a problem with the request.');//if it fails report the fail
    }
  }
}

when i console.log the extracted div from the JSON it shows up fine with the contents:

console.log(disp);
//returns:
<div id=aboutnf><p>lorem ipsum</p></div>

console.log(typeof(disp));
//returns:
object

but after converting to a string:

console.log(dispContents);
//returns:
[object HTMLDivElement]

and inserting it to the page shows the same thing: an empty HTMLdivElement

document.getElementById('stuff').innerHTML = dispContents;
//puts:
[object HTMLDivElement]
//on the page instead of the contents of the div

how do i convert the JSON object into a string without losing all its contents?

i would prefer not to use jQuery for this.

Edit:

I used JsonObj.content.rendered and then appendchild()
to get the div I wanted from the json

thanks Nesaje, Patrick Roberts and nnnnnn

3
  • Don't convert it to a string, it's easier and more efficient to simply use document.getElementById('stuff').appendChild(disp);. Commented Nov 3, 2017 at 8:24
  • As I see it, you're not extracting your html correctly. So, instead of var content = JsonObj.content; try this var content = JsonObj.content.rendered; In that case your content variable will hold your html string without need to stringify or whatever. Just use it as it is. Commented Nov 3, 2017 at 8:28
  • You don't want JSON.stringify(), that turns an object back into a JSON string. The object you got via JSON.parse() has the value you need in a property JsonObj.content.rendered. Commented Nov 3, 2017 at 8:31

1 Answer 1

1

I didn't quite catch your question very well. But what I understood is that you want to display the contents of rendered in the json data.

{
  "content": {
    "rendered": "<div id=aboutnf><p>lorem ipsum</p></div>",
    "protected": false
  }
}

I would rewrite your function like this

function displayContents() {
  //if the request succeeds display the contents
  if (httpRequest.readyState === XMLHttpRequest.DONE) {
    if (httpRequest.status === 200) {
      //parse the JSON
      var JsonObj = JSON.parse(httpRequest.responseText);//parse the response
      var content = JsonObj.content;
      document.getElementById('stuff').innerHTML = content.rendered;
      //if the request failed report it
    } else {
      alert('There was a problem with the request.');//if it fails report the fail
    }
  }
}

That would automatically display the contents of rendered in the html field.

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.