Javascript 如何将字符串数组解析为JSON?

Javascript 如何将字符串数组解析为JSON?,javascript,json,node.js,infusionsoft,Javascript,Json,Node.js,Infusionsoft,我最难让这个字符串被解析。这似乎是一项简单的任务,但它让我发疯。Infusionsoft将此作为其rest钩子负载返回,因此我无法更改其接收方式 JSON.parse()不起作用,我无法将其作为对象文本使用,因为时间戳没有引号。有没有一种方法或方法我刚才没有看到,可以解析它,这样我就可以很容易地得到每个id,以for循环为例 [{id:1049105,api_url:'',时间戳:2017-07-12T00:34:36.000Z},{id:993221,api_url:'',时间戳:2017-0

我最难让这个字符串被解析。这似乎是一项简单的任务,但它让我发疯。Infusionsoft将此作为其rest钩子负载返回,因此我无法更改其接收方式

JSON.parse()
不起作用,我无法将其作为对象文本使用,因为时间戳没有引号。有没有一种方法或方法我刚才没有看到,可以解析它,这样我就可以很容易地得到每个
id
,以for循环为例

[{id:1049105,api_url:'',时间戳:2017-07-12T00:34:36.000Z},{id:993221,api_url:'',时间戳:2017-07-12T00:34:18.000Z}]


任何帮助都将不胜感激。

通过一些字符串操作和对字符串部分的迭代,我们可以将此响应解析为有效的JS对象数组

我运行了下面的代码,得到了一个JS对象数组,这些对象映射到数组字符串中的每个“对象”。它还会将无效的时间戳值转换为JS日期对象

let args = "[{id:1049105, api_url:'', timestamp: 2017-07-12T00:34:36.000Z},{id:993221, api_url:'', timestamp: 2017-07-12T00:34:18.000Z}]"

let splitArgs = args.split('},')
// Create an Array of parsed Objects
let objs = splitArgs.map(arg => {
    // remove whitespace
    let cleanArg = arg.trim()

    // Remove enclosing [ { } ] characters
    if (arg.startsWith('[')) {
        cleanArg = cleanArg.substr(1, arg.length)
    }
    if (cleanArg.startsWith('{')) {
        cleanArg = cleanArg.substr(1, arg.length)
    }
    if (cleanArg.endsWith(']')) {
        cleanArg = cleanArg.substr(0, arg.length - 1)
    }
    if (cleanArg.endsWith('}')) {
        cleanArg = cleanArg.substr(0, arg.length - 1)
    }

    // Remove any quotations and then split each of the properties out  
    let props = cleanArg.replace(/[\']+/, '').split(',')

    // For each prop, get the value and assign it to the new object
    // that will be returned by reduce()
    return props.reduce((obj, prop) => {
        let splitIndex = prop.indexOf(':')
        let key = prop.substr(0, splitIndex)
        let val = prop.substr(splitIndex + 1, prop.length)

        if (key.toLowerCase() === 'timestamp') {
            obj[key] = (new Date(val))
        } else {
            obj[key] = val
        }
        return obj
    }, {})
})

console.log(objs.map(obj => { return obj.id })) // [1049105, 993221]

日期是ISO格式的,因此可以使用几个正则表达式将字符串预处理为JSON

string = "[...]"; 
string.replace(/(\d{4}-\d_2+-\d{2} ... /g, '"$1"'); // enclose dates in quotes
string.replace(/'/g, '"'); // replace single quotes with double quotes
string.replace(/id/g, '"id"'); // enclose id in double quotes
// repeat for api_url and timestamp

data = JSON.parse(string);
我是,你想试试这个吗:

const {sscanf} = require('scanf');

let str = `[{id:1049105, api_url:'', timestamp: 2017-07-12T00:34:36.000Z},{id:993221, api_url:'', timestamp: 2017-07-12T00:34:18.000Z}]`;
let chunks = str.split('},{');

for (let chunk of chunks) {
  let obj = sscanf(chunk, "id:%d, api_url:%s, timestamp: %s}", 'id', 'api_url', 'timestamp')
  console.log(obj);
}

/*
{ id: 1049105,
  api_url: '\'\'',
  timestamp: '2017-07-12T00:34:36.000Z' }
{ id: 993221,
  api_url: '\'\'',
  timestamp: '2017-07-12T00:34:18.000Z' }
*/

可能是由于使用或BUG方面的问题,您可以提交更多信息。

这根本不是有效的JSON,属性周围都应该有引号才能有效。您正在尝试解析整个内容吗?你能说得更清楚一点吗?我感觉你从API中得到了有效的JSON,但它已经被解析了。例如,如果您正在使用
jQuery.ajax
并指定
json
数据类型来向服务器发送请求。在这种情况下,您可能已将解析对象的日志输出粘贴到问题中。如果恰好是这种情况,那么您根本不需要
JSON.parse()
。如果不是这种情况,我建议粘贴一个代码示例,以便更容易地看到您在做什么。@Marconius我很确定它是以字符串形式出现的,而不是经过解析的JSON。当webhook出现时,初始负载被bodyparser解析为JSON,我得到这个负载:
{event\u key:'contact.edit',object\u type:'contact',object\u key:'[{id:1057781,api\u url:\'\',时间戳:2017-07-11T19:24:41.000Z},{id:1035169,api\u url:\'''\',时间戳:2017-07-11T19:23:56.000Z},api\u url:'}
如您所见,
对象\u键
表示为字符串。我试着用JSON.parse解析那个部分,但它返回了一个错误。@BritGwaltney你检查过我更新的答案了吗?