SoFunction
Updated on 2025-03-03

js Object2String is convenient to view js object content

js Object2String is convenient to view js object content

Updated: November 24, 2014 18:47:11 Submission: mdxy-dxy
This article mainly introduces the method of outputting any JS object into a json format string. Friends who need it can refer to it
<script type="text/javascript">
  /**
    * Output any JS object as a json format string
    * @param {Object} _obj: Object that needs to be output as string
    */
  var obj2String = function(_obj) {
    var t = typeof (_obj);
    if (t != 'object' || _obj === null) {
      // simple data type
      if (t == 'string') {
        _obj = '"' + _obj + '"';
      }
      return String(_obj);
    } else {
      if ( _obj instanceof Date) {
        return _obj.toLocaleString();
      }
      // recurse array or object
      var n, v, json = [], arr = (_obj && _obj.constructor == Array);
      for (n in _obj) {
        v = _obj[n];
        t = typeof (v);
        if (t == 'string') {
          v = '"' + v + '"';
        } else if (t == "object" && v !== null) {
          v = this.obj2String(v);
        }
        (( arr ? '' : '"' + n + '":') + String(v));
      }
      return ( arr ? '[' : '{') + String(json) + ( arr ? ']' : '}');
    }
  };
  var obj = {
    "result" : {
      "fs" : {
        "" : [{
          "_value" : "1.0",
          "_class" : 4
        }],
        ".GET_FNAMES" : [{
          "_value" : "0.0",
          "_class" : 4
        }],
        ".GET_TOKEN_ID" : [{
          "_value" : "0.0",
          "_class" : 4
        }],
        "" : [{
          "_value" : "0.0",
          "_class" : 4
        }]
      }
    },
    "isCanceled" : false,
    "e" : "",
    "isResponsed" : true,
    "aoqSize" : 0,
    "isAsyncPost" : false,
    "code" : 0,
    "reqUID" : "xxxx-xxxxxx-xxxxx-6c2f17bb-ea18-42ec-98fa-3f63b8d26aba-nd-rq",
    "version" : "1.0",
    "fName" : ".GET_FNAMES",
    "message" : "Successfully obtained 4 Features",
    "dir" : "DOWN",
    "nodeTime" : 1362462128706,
    "isKeyCompressed" : false,
    "seq" : 2
  }
  alert(obj2String(obj))
</script>
  • Any object
  • json

Related Articles

  • JavaScript DOM node addition example

    This section introduces the addition of JavaScript DOM nodes, and put the str in node into the h1 paragraph to make it a new paragraph of h1
    2014-07-07
  • js application in three cases: frame page jump (target)

    This article mainly introduces the jump to the frame page: jump out of the frame, on the parent page; jump from a frame to the frame with name=main; ContentList The iframe name of the current page. Interested friends can learn about it.
    2013-04-04
  • Share 10 CSS and JavaScript tools to optimize code

    If you want to keep the file or execute the lint code at the stage, the linting tool can also do as you wish. It depends on the individual's choice. If you are looking for the best linting tools for CSS and JavaScript, then continue reading
    2016-05-05
  • JS implements the analysis of the method of removing specified substrings from strings

    This article mainly introduces the method of removing specified substrings from strings by JS. It analyzes the operation techniques related to JavaScript using string substring replacement, segmentation and aggregation based on the example form. Friends who need it can refer to it
    2018-05-05
  • How to implement Javascript Request to get request parameters

    When using Javascript Request to obtain parameters, there is always an error. This article provides a detailed solution to this problem. Friends who need to know can refer to it.
    2012-11-11
  • Three ways to realize waterfall flow layout

    This article mainly introduces the method of implementing waterfall flow layout using javascript, jquery, and css respectively. It has good reference value, let's take a look with the editor below
    2017-02-02
  • Example tutorial for using js bit operation in practice

    We may rarely use bit operations in programming. If we don’t study in depth, it may be difficult to understand. The following article mainly introduces relevant information about the actual use of js bit operations. Friends who need it can refer to it.
    2022-03-03
  • Detailed explanation of examples about recursion and backtracking in JavaScript

    This article will mainly introduce the principles and uses of recursion and backtracking in JavaScript. The article introduces in detail through some examples. Interested friends can follow the editor to learn it.
    2022-07-07
  • JavaScript uses canvas to implement mouse drag and drop function

    This article mainly introduces in detail JavaScript to use canvas to implement mouse drag and drop function. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2020-07-07
  • JS implementation method of using chain attribute expressions to obtain values ​​and assignments

    This article mainly introduces in detail how JS uses chain attribute expressions to get values ​​and assign values. The article introduces the code examples in detail, which is of some help to our study or work. Interested students can refer to it.
    2023-08-08

Latest Comments