如何将JSON与特定路径进行字符串化?

时间:2018-01-25 18:12:15

标签: javascript json node.js

我过去曾使用JSON.stringify();,但似乎无法为我的JSON指定特定路径。当我运行此代码时:

socket.emit('m', {
    author = res.hits.['_source'].author;
    description: "<td>" + JSON.stringify(author) + "</td>",
});

我收到以下错误:

author = res.hits.['_source'].author;
                  ^
SyntaxError: Unexpected token [
    at createScript (vm.js:80:10)

2 个答案:

答案 0 :(得分:2)

我认为JSON.stringify()不会导致问题。

看起来你混合了两种表示法样式,点符号和括号表示法。如果你删除['_source']前面的点,我认为应该可行。

author = res.hits['_source'].author;

答案 1 :(得分:0)

你有res对象看起来与此类似吗?

res =  { hits: { _source: { author: 'xyz'}}};

如果是,那么你做错了,无法达到author你需要这样做

author = res.hits['_source'].author;