JavaScript Formatting a string to JSON Object to disply in HTML In a textarea w/ [(ngModel)] formatting json string,formatting json string in java,formatting json

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in a straight line instead of formatting . I also tried <pre> {{formatJson | json}}</pre> and still no success.

formatJSON: string  = '{"a":1,"b":2,"c":{"d":3, "e":4}}'

ngOnInit() {
  var test = json.parse(this.formatJSON);
  console.log(typeof test); //string
  this.formatJSON = JSON.stringify(test, null, "   ")

}

HTML Code:

<div>
  <textarea [(ngModel)]="formatJSON" class="text-area" cols="200" rows="10">
       {{formatJSON}}
  </textarea>
</div>


<!-- <pre>{{formatJSON | json}}</pre> -->

Desired Output:

Desired Output

Answer:1

Try this:

var data = {"a":1,"b":2,"c":{"d":3,"e":4}}


document.getElementById("json-textArea").innerHTML = JSON.stringify(data, undefined, 2);
textarea { 

width:100px;
height: 150px;

}
<textarea id="json-textArea"></textarea>
Answer:2

Please try this. Update your JSON string like below

formatJSON = {
    "a": 1,
    "b": 2,
    "c": {
        "d": 3,
        "e": 4
    }
}

You can apply angular pipe on formatJSON in your html like this {{formatJSON | json}}.

Answer:3

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

  1. facebook game invite
  2. facebook game invite friends
  3. facebook instant game invite
  4. facebook instant game invite friend

I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...

I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...

  1. discord force user into voice channel

I'm currently writing an e2e test and I would like to create some classes which abstract certain async tasks for me. In the end I would like to instantiate an object, which let's me chain async ...

I'm currently writing an e2e test and I would like to create some classes which abstract certain async tasks for me. In the end I would like to instantiate an object, which let's me chain async ...

  1. function chaining with return
  2. function chaining in javascript
  3. function chaining in c++
  4. function chaining in python
  5. function chaining in java
  6. function chaining in jquery
  7. function chaining in node js
  8. function chaining in php
  9. function chaining in java 8
  10. function chaining in js
  11. function chaining in scala
  12. function chaining in rust
  13. distributed function chaining with anycast routing

Is Array.from(arguments) worse in some way then Array.prototype.slice.call(arguments) for creating an array of arguments? I haven't seen the former used anywhere, and the latter seems to be the ...

Is Array.from(arguments) worse in some way then Array.prototype.slice.call(arguments) for creating an array of arguments? I haven't seen the former used anywhere, and the latter seems to be the ...