javascript 将对象转换为字符串并返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31262457/
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
Convert Object to string and back
提问by Dmitrij Holkin
I need to convert Javascript object to string and then this string back to object.
我需要将 Javascript 对象转换为字符串,然后将此字符串转换回对象。
Objects i get like that:
我得到这样的对象:
var Checked = {};
// Hold all checkboxes
$('div.list input[type=radio]:checked, input[type=checkbox]:checked').each(function () {
var $el = $(this);
var name = $el.attr('name');
if (typeof (Checked[name]) === 'undefined') {
Checked[name] = [];
}
Checked[name].push($el.val());
});
I know how to do this with array by using join and split, but how to be with objects? Now how to convert this object to string? How to get back this string to object?
我知道如何通过使用 join 和 split 来处理数组,但是如何处理对象呢?现在如何将此对象转换为字符串?如何取回这个字符串来反对?
回答by hungndv
Here you are:
这个给你:
var object = {
"1": [1, 2, {
3: "3"
}]
};
var str = JSON.stringify(object);
console.log(str);
var obj = JSON.parse(str);
console.log(obj["1"][2][3]);
Hope this helps.
希望这可以帮助。
回答by Mohit Kanwar
The JSON.parse()
method parses a string as a JSON object, optionally transforming the value produced by parsing.
该JSON.parse()
方法将字符串解析为 JSON 对象,可选择转换解析生成的值。
Syntax
句法
JSON.parse(text[, reviver])
JSON.parse(text[, reviver])
Parameters
参数
text The string to parse as JSON. See the JSON object for a description of JSON syntax. reviver Optional If a function, prescribes how the value originally produced by parsing is transformed, before being returned.
text 要解析为 JSON 的字符串。有关 JSON 语法的说明,请参阅 JSON 对象。reviver 可选 如果是函数,则规定在返回之前如何转换最初由解析产生的值。
Returns
退货
Returns the Object corresponding to the given JSON text.
返回与给定 JSON 文本对应的 Object。
Throws
投掷
Throws a SyntaxError exception if the string to parse is not valid JSON.
如果要解析的字符串不是有效的 JSON,则抛出 SyntaxError 异常。
The JSON.stringify()
method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
该JSON.stringify()
方法将 JavaScript 值转换为 JSON 字符串,如果指定了替换器函数,则可以选择替换值,或者如果指定了替换器数组,则可以选择仅包含指定的属性。
Syntax
句法
JSON.stringify(value[, replacer[, space]])
JSON.stringify(value[, replacer[, space]])
Parameters
参数
value
价值
The value to convert to a JSON string.
replacer(Optional)
替代品(可选)
A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.
改变字符串化过程行为的函数,或作为白名单的 String 和 Number 对象数组,用于选择要包含在 JSON 字符串中的值对象的属性。如果此值为 null 或未提供,则对象的所有属性都包含在生成的 JSON 字符串中。
space(Optional)
空间(可选)
A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 if it's larger than that. Values less than 1 indicate that no space should be used. If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.
一个 String 或 Number 对象,用于在输出 JSON 字符串中插入空格以提高可读性。如果这是一个数字,则表示用作空格的空格字符数;如果大于此数字,则此数字的上限为 10。小于 1 的值表示不应使用空格。如果这是一个字符串,则该字符串(或字符串的前 10 个字符,如果它长于该字符)用作空白。如果未提供此参数(或为空),则不使用空格。
Source:
来源:
回答by PHP Worm...
var obj = { x: 5, y: 6 };
var a = JSON.stringify(obj);
console.log(typeof a);
console.log( a);
var b = $.parseJSON(a);
console.log(typeof b);
console.log( b);