admin管理员组文章数量:1277903
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:
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:
Share Improve this question edited Apr 1, 2019 at 16:06 Flash asked Apr 1, 2019 at 14:49 FlashFlash 1,0145 gold badges26 silver badges47 bronze badges 6- Possible duplicate of Angular 2 pipe that transforms JSON object to pretty-printed JSON – peinearydevelopment Commented Apr 1, 2019 at 14:51
- 3 Your string isn't valid JSON. – Mark Commented Apr 1, 2019 at 14:52
-
1
JSON is basically a map from
string
toany
. But in your case, your fields are not string which makes your JSON invalid. – Onur Arı Commented Apr 1, 2019 at 14:52 - What do you mean my fields are not string? and what would be the solution then? – Flash Commented Apr 1, 2019 at 14:56
- Possible duplicate of Pretty Json object in html – nircraft Commented Apr 1, 2019 at 15:07
4 Answers
Reset to default 8Try 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>
check this stackblitz for the angular version: In Angular you just need to run your JSON data through json
pipe and you will be fine.
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}}
.
I have applied it in angular Dialog material like this
in ts file
this.response=JSON.stringify(JSON.parse(item.response_body), null, 2);
in html
<mat-dialog-content>
<pre class="response-class"> {{data.response}} </pre>
</mat-dialog-content>
in css
::ng-deep .response-class {
white-space: pre-wrap;
}
It will work fine if it's a valid json, you can use pretty json to check from it http://jsonprettyprint./
This is an easy way to show json, with the json pipe behaves the same as doing JSON.stringify(yourData)
in your code
<pre>{{ yourData | json }}</pre>
版权声明:本文标题:javascript - Formatting a string to JSON Object to disply in HTML In a textarea w [(ngModel)] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741249329a2365496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论