无法解析node.js中的json对象

时间:2018-10-12 09:00:04

标签: javascript node.js

我正在尝试迭代json的每个元素,但无法打印。 我也很累JSON.parse()。

代码:

n

以上代码将每个字符打印为 enter image description here

3 个答案:

答案 0 :(得分:2)

格式化字符串时我注意到的是这个

{
 "Type": "INFO ",
  "TimeStamp": "2018-10-10 12:05:16.881",
  "IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
  "MacId": "84:7B:EB:3F:65:A4",
  "OS": "Microsoft Windows 10 Home Single Language",
  "Method": "SyncData",
  "LineNo": 527,
  "Message": "SyncData: Billing Software SyncData() called"
}\n{
  "Type": "INFO ",
  "TimeStamp": "2018-10-10 12:05:17.060",
  "IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
  "MacId": "84:7B:EB:3F:65:A4",
  "OS": "Microsoft Windows 10 Home Single Language",
  "Method": "SyncData",
  "LineNo": 38,
  "Message": "SyncData: Start"
}\n{
  "Type": "INFO ",
  "TimeStamp": "2018-10-10 12:05:17.111",
  "IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
  "MacId": "84:7B:EB:3F:65:A4",
  "OS": "Microsoft Windows 10 Home Single Language",
  "Method": "SyncData",
  "LineNo": 44,
  "Message": "SyncData: LocalConnectionString = Data Source=PARAM\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;"
}

要对此进行处理,您可以执行以下操作

const j = "{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:16.881\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":527,\"Message\":\"SyncData: Billing Software SyncData() called\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.060\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":38,\"Message\":\"SyncData: Start\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.111\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":44,\"Message\":\"SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;\"}";  

//first remove the \n and join with a comma ,
// enclose your multiple object within an array
const joinedWithComma = "[" + j.split('\n').join(',') + "]"


//now parse this
const parsedValue = JSON.parse(joinedWithComma)


console.log(parsedValue)

答案 1 :(得分:1)

您应该解析字符串并使用方法JSON.parse()转换为JSON对象。

编辑

我注意到您的字符串有几个JSON对象,因此在传递给\r\n方法之前,应先用JSON.parse分割字符串。

var j = "{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:16.881\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":527,\"Message\":\"SyncData: Billing Software SyncData() called\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.060\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":38,\"Message\":\"SyncData: Start\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.111\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":44,\"Message\":\"SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;\"}";
var list = j.split('\r\n');
for (var i = 0; i < list.length; ++i) {
  var obj = JSON.parse(list[i]);
  for(var key in obj) {
    console.log(obj[key]);
  }
}

答案 2 :(得分:1)

很明显,您正在迭代给定字符串的字符。该字符串为JSON格式,但仍然是一个字符数组。

此外,您的JSON无效。清理之后,我注意到它是JSON对象的JSON数组:

const os = JSON.parse(`[{
		"Type": "INFO ",
		"TimeStamp": "2018-10-10 12:05:16.881",
		"IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
		"MacId": "84:7B:EB:3F:65:A4",
		"OS": "Microsoft Windows 10 Home Single Language",
		"Method": "SyncData",
		"LineNo": 527,
		"Message": "SyncData: Billing Software SyncData() called"
	},
	{
		"Type": "INFO ",
		"TimeStamp": "2018-10-10 12:05:17.060",
		"IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
		"MacId": "84:7B:EB:3F:65:A4",
		"OS": "Microsoft Windows 10 Home Single Language",
		"Method": "SyncData",
		"LineNo": 38,
		"Message": "SyncData: Start"
	},
	{
		"Type": "INFO ",
		"TimeStamp": "2018-10-10 12:05:17.111",
		"IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
		"MacId": "84:7B:EB:3F:65:A4",
		"OS": "Microsoft Windows 10 Home Single Language",
		"Method": "SyncData",
		"LineNo": 44,
		"Message": "SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;"
	}
]`)


for (let o of os)
  for (let key in o)
    console.log(key, ' = ', o[key])