Verifies whether a string is in JSON format and presents a regular presentation of a malformed JSON-formatted string (JSON formatted)

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Directory (?) [+]

[HTML]View PlainCopy 
    1. var str = {"Code": "", "Svcname": "", "Version": "", "Component": "", "category": "", "requestmsg": [{"FieldName": " UserName ', ' type ': ' Simple ', ' required ': ' True '},{' fieldName ': ' Age ', ' type ': ' Simple ', ' required ': ' True '}]}

First, check if the string is in JSON format: [HTML]View PlainCopy 
    1. function Isjsonformat (str) {
    2. try {
    3. $.parsejson (str);
    4. } catch (e) {
    5. return false;
    6. }
    7. return true;
    8. }


Format the JSON-formatted string output, first convert the JSON string to an object, and then format the object in JSON output: [HTML]View PlainCopy  
    1. Json.stringify (Json.parse (JSON), null, "\ T")
[HTML]View PlainCopy 
    1. Json.stringify (Json.parse (JSON), NULL, 4)



Ps:\t: Represents indenting a tab;4: Represents the output formatted after indenting 4 spaces: [HTML]View PlainCopy 
  1. {
  2. "Code": "",
  3. "Svcname": "",
  4. "Version": "",
  5. "Component": "",
  6. "Category": "",
  7. "Requestmsg": [
  8. {
  9. "FieldName": "UserName",
  10. "Type": "Simple",
  11. "Required": "True"
  12. },
  13. {
  14. "FieldName": "Age",
  15. "Type": "Simple",
  16. "Required": "True"
  17. }
  18. ]
  19. }


Recommend a good JSON online parsing tool: http://www.json.cn/

Verifies whether a string is in JSON format and presents a regular presentation of a malformed JSON-formatted string (JSON formatted)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.