如何将JSON字符串转换为JavaScript对象,包括类型检查

时间:2022-08-26 11:58:59

For a Javascript project I have an json string converted into a Javascript object. But the type of all my values is 'string' becaus of the JSON parsing.
Is there any solution to identify the types and let a script convert them into the correct javascript type?

对于Javascript项目,我将json字符串转换为Javascript对象。但是我所有值的类型都是'字符串',因为JSON解析。是否有任何解决方案来识别类型,并让脚本将它们转换为正确的javascript类型?

for example

//Javascript object for the json decoded string    
var jsonObj = { id: "foo", count: "1" };

All the values are of type 'string' but I want count to be seen as a number. Is there a parser to set the correct type or does it need to be done manual in JS?

所有值都是'string'类型,但我希望count被视为一个数字。是否有解析器来设置正确的类型,还是需要在JS中手动完成?

1 个解决方案

#1


1  

You can use a reviver with JSON.parse.

您可以使用带有JSON.parse的reviver。

json2.js describes the reviver thus

json2.js因此描述了复活者

JSON.parse(text, reviver)

The optional reviver parameter is a function that can filter and transform the results. It receives each of the keys and values, and its return value is used instead of the original value. If it returns what it received, then the structure is not modified. If it returns undefined then the member is deleted.

可选的reviver参数是一个可以过滤和转换结果的函数。它接收每个键和值,并使用其返回值而不是原始值。如果它返回它收到的内容,则不修改结构。如果它返回undefined,则删除该成员。

So to convert count to a number you might do

因此,要将计数转换为您可能会执行的数字

JSON.parse(myJsonString, function (key, value) {
  return key === "count" ? +value : value;
});

so

JSON.stringify(JSON.parse('{ "id": "foo", "count": "3" }', function (key, value) {
  return key === "count" ? +value : value;
}));

produces

{"id":"foo","count":3}

EDIT

To handle dates as well, you can

要处理日期,你也可以

JSON.parse(myJsonString, function (key, value) {
  // Don't muck with null, objects or arrays.
  if ("object" === typeof value) { return value; }
  if (key === "count") { return +value; }
  // Unpack keys like "expirationDate" whose value is represented as millis since epoch.
  if (/date$/i.test(key)) { return new Date(+value); }
  // Any other rules can go here.
  return value;
});

#1


1  

You can use a reviver with JSON.parse.

您可以使用带有JSON.parse的reviver。

json2.js describes the reviver thus

json2.js因此描述了复活者

JSON.parse(text, reviver)

The optional reviver parameter is a function that can filter and transform the results. It receives each of the keys and values, and its return value is used instead of the original value. If it returns what it received, then the structure is not modified. If it returns undefined then the member is deleted.

可选的reviver参数是一个可以过滤和转换结果的函数。它接收每个键和值,并使用其返回值而不是原始值。如果它返回它收到的内容,则不修改结构。如果它返回undefined,则删除该成员。

So to convert count to a number you might do

因此,要将计数转换为您可能会执行的数字

JSON.parse(myJsonString, function (key, value) {
  return key === "count" ? +value : value;
});

so

JSON.stringify(JSON.parse('{ "id": "foo", "count": "3" }', function (key, value) {
  return key === "count" ? +value : value;
}));

produces

{"id":"foo","count":3}

EDIT

To handle dates as well, you can

要处理日期,你也可以

JSON.parse(myJsonString, function (key, value) {
  // Don't muck with null, objects or arrays.
  if ("object" === typeof value) { return value; }
  if (key === "count") { return +value; }
  // Unpack keys like "expirationDate" whose value is represented as millis since epoch.
  if (/date$/i.test(key)) { return new Date(+value); }
  // Any other rules can go here.
  return value;
});