Javascript jquery json 到字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3593046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:21:26  来源:igfitidea点击:

jquery json to string?

javascriptjqueryjson

提问by Incognito

Instead of going from a json string and using $.parseJSON, I need to take my object and store it in a variable as string representing json.

我不需要从 json 字符串开始并使用 $.parseJSON,而是需要将我的对象作为表示 json 的字符串存储在一个变量中。

(A library I'm dealing with expects a malformed json type so I need to mess around with it to get it to work.)

(我正在处理的一个库需要一个格式错误的 json 类型,所以我需要弄乱它才能让它工作。)

What's the best way to do this?

做到这一点的最佳方法是什么?

回答by Inigoesdr

Edit:You should use the json2.js library from Douglas Crockford instead of implementing the code below. It provides some extra features and better/older browser support.

编辑:您应该使用 Douglas Crockford 的 json2.js 库,而不是实现下面的代码。它提供了一些额外的功能和更好/更旧的浏览器支持。

Grab the json2.js file from: https://github.com/douglascrockford/JSON-js

从以下位置获取 json2.js 文件:https: //github.com/douglascrockford/JSON-js



// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
        // simple data type
        if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    }
    else {
        // 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 = JSON.stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
};

var tmp = {one: 1, two: "2"};
JSON.stringify(tmp); // '{"one":1,"two":"2"}'

Code from: http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/

代码来自:http: //www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/

回答by jonathan

I use

我用

$.param(jsonObj)

which gets me the string.

这让我得到了字符串。

回答by AlexChaffee

Most browsers have a native JSONobject these days, which includes parseand stringifymethods. So just try JSON.stringify({})and see if you get "{}". You can even pass in parameters to filter out keys or to do pretty-printing, e.g. JSON.stringify({a:1,b:2}, null, 2)puts a newline and 2 spaces in front of each key.

如今,大多数浏览器都有一个本机JSON对象,其中包括parsestringify方法。所以试试看JSON.stringify({})你是否得到"{}". 您甚至可以传入参数来过滤键或进行漂亮的打印,例如JSON.stringify({a:1,b:2}, null, 2)在每个键前放置一个换行符和 2 个空格。

JSON.stringify({a:1,b:2}, null, 2)

gives

"{\n  \"a\": 1,\n  \"b\": 2\n}"

which prints as

打印为

{
  "a": 1,
  "b": 2
}

As for the messing around part of your question, use the second parameter. From http://www.javascriptkit.com/jsref/json.shtml:

至于问题的一部分,请使用第二个参数。从http://www.javascriptkit.com/jsref/json.shtml

The replacer parameter can either be a function or an array of String/Numbers. It steps through each member within the JSON object to let you decide what value each member should be changed to. As a function it can return:

  • A number, string, or Boolean, which replaces the property's original value with the returned one.
  • An object, which is serialized then returned. Object methods or functions are not allowed, and are removed instead.
  • Null, which causes the property to be removed.

As an array, the values defined inside it corresponds to the names of the properties inside the JSON object that should be retained when converted into a JSON object.

replacer 参数可以是函数或字符串/数字数组。它遍历 JSON 对象中的每个成员,让您决定每个成员应该更改为什么值。作为一个函数,它可以返回:

  • 一个数字、字符串或布尔值,用返回的值替换属性的原始值。
  • 一个对象,它被序列化然后返回。不允许使用对象方法或函数,而是将其删除。
  • Null,这会导致该属性被删除。

作为一个数组,其中定义的值对应于JSON对象内部的属性名称,转换为JSON对象时应该保留。

回答by David Reynolds

The best way I have found is to use jQuery JSON

我发现的最好方法是使用jQuery JSON

回答by cofiem

You could parse the JSON to an object, then create your malformed JSON from the ajavscript object. This may not be the best performance-wise, tho.

您可以将 JSON 解析为一个对象,然后从 ajavscript 对象创建格式错误的 JSON。这可能不是最好的性能,不过。

Otherwise, if you only need to make very small changes to the string, just treat it as a string, and mangle it using standard javascript.

否则,如果您只需要对字符串进行非常小的更改,只需将其视为字符串,并使用标准 javascript 对其进行处理。