【问题标题】:Cant get the title from the following json无法从以下 json 中获取标题
【发布时间】:2016-10-15 09:48:20
【问题描述】:

我的应用中有这段代码,它以 json 格式返回一些工作提要:

我的代码如下:

JobFeed.find().sort('-created').exec(function (err, feeds) {
    if (err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {

        _.forEach(feeds, function (row, key) {
            console.log(row.data.title); // showing undefined
        });

        res.jsonp(feeds);
    }
});

json 如下所示:

  { data:
   { isSaved: null,
     client:
      { lastContractTitle: null,
        feedbackText: '4.78 Stars, based on 13 feedbacks',
        paymentVerificationStatus: 1,
        lastContractPlatform: null,
        totalFeedback: 4.7819242373,
        location: [Object],
        lastContractRid: 0,
        edcUserId: 3728985,
        totalReviews: 13,
        companyRid: 0,
        spentTier: '$7,500+',
        companyName: null },
     amount: { amount: 0, currencyCode: 'USD' },
     createdOn: '2016-10-13T06:30:54+00:00',
     skills: [ [Object], [Object], [Object], [Object] ],
     enterpriseJob: false,
     ciphertext: '~01026b31d972ed826b',
     proposalsTier: null,
     description: 'Hello! We\'re currently searching for a developer with',
     category2: 'Web, Mobile & Software Dev',
     type: 2,
     tierText: 'Intermediate ($$)',
     relevance:
      { hoursInactive: 0,
        id: 208660674,
        publishTime: '1476340265000',
        recommendedEffectiveCandidates: 2,
        effectiveCandidates: 3,
        uniqueImpressions: 0 },
     isApplied: null,
     engagement: '30+ hrs/week',
     recno: 208660674,
     title: 'Server-side and Client-side Game Developer for Indie MMO',
     freelancersToHire: 0,
     maxAmount: { amount: 0, currencyCode: 'USD' },
     duration: 'More than 6 months',
     subcategory2: 'Game Development',
     sticky: false,
     stickyLabel: '',
     feedback: null },
  id: 57ffcdbf717ca50cf0b4cbc1 }
{ data:
   { isSaved: null,
     client:
      { lastContractTitle: null,
        feedbackText: 'No feedback yet',
        paymentVerificationStatus: 1,
        lastContractPlatform: null,
        totalFeedback: 0,
        location: [Object],
        lastContractRid: 0,
        edcUserId: 0,
        totalReviews: 0,
        companyRid: 0,
        spentTier: '$100+',
        companyName: null },
     amount: { amount: 0, currencyCode: 'USD' },
     createdOn: '2016-10-13T06:30:51+00:00',
     skills: [ [Object], [Object], [Object] ],
     enterpriseJob: false,
     ciphertext: '~01c59efb9135ed91d0',
     proposalsTier: null,
     description: 'Hello! We\'re currently searching for a developer with',
     category2: 'Web, Mobile & Software Dev',
     type: 2,
     tierText: 'Entry Level ($)',
     relevance:
      { hoursInactive: 0,
        id: 208660673,
        publishTime: '1476340266000',
        recommendedEffectiveCandidates: 18,
        effectiveCandidates: 74,
        uniqueImpressions: 0 },
     isApplied: null,
     engagement: 'Less than 10 hrs/week',
     recno: 208660673,
     title: 'Wordpress & Stripe developer',
     freelancersToHire: 0,
     maxAmount: { amount: 0, currencyCode: 'USD' },
     duration: 'More than 6 months',
     subcategory2: 'Web Development',
     sticky: false,
     stickyLabel: '',
     feedback: null },
  id: 57ffcdbf717ca50cf0b4cbc2 }
{ data:
   { isSaved: null,
     client:
      { lastContractTitle: null,
        feedbackText: 'No feedback yet',
        paymentVerificationStatus: 5,
        lastContractPlatform: null,
        totalFeedback: 0,
        location: [Object],
        lastContractRid: 0,
        edcUserId: 0,
        totalReviews: 0,
        companyRid: 0,
        spentTier: 'Less than $100',
        companyName: null },
     amount: { amount: 2000, currencyCode: 'USD' },
     createdOn: '2016-10-13T06:30:18+00:00',
     skills: [ [Object], [Object] ],
     enterpriseJob: false,
     ciphertext: '~0101449f91585e8e16',
     proposalsTier: null,
     description: 'I need a Christmas card app before 15th Dec.n',
     category2: 'Web, Mobile & Software Dev',
     type: 1,
     tierText: 'Intermediate ($$)',
     relevance:
      { hoursInactive: 0,
        id: 208660669,
        publishTime: '1476340233000',
        recommendedEffectiveCandidates: 7,
        effectiveCandidates: 52,
        uniqueImpressions: 0 },
     isApplied: null,
     engagement: null,
     recno: 208660669,
     title: 'Christmas Card iPhone App',
     freelancersToHire: 0,
     maxAmount: { amount: 0, currencyCode: 'USD' },
     duration: null,
     subcategory2: 'Mobile Development',
     sticky: false,
     stickyLabel: '',
     feedback: null },
  id: 57ffcdbf717ca50cf0b4cbc3 }
{ data:
   { isSaved: null,
     client:
      { lastContractTitle: null,
        feedbackText: '4.88 Stars, based on 102 feedbacks',
        paymentVerificationStatus: 1,
        lastContractPlatform: null,
        totalFeedback: 4.8843944128,
        location: [Object],
        lastContractRid: 0,
        edcUserId: 0,
        totalReviews: 102,
        companyRid: 0,
        spentTier: '$25,000+',
        companyName: null },
     amount: { amount: 100, currencyCode: 'USD' },
     createdOn: '2016-10-13T06:30:00+00:00',
     skills:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     enterpriseJob: false,
     ciphertext: '~01a04eab3b05372652',
     proposalsTier: null,
     description: 'Hello! We\'re currently searching for a developer with',
     category2: 'Writing',
     type: 1,
     tierText: 'Entry Level ($)',
     relevance:
      { hoursInactive: 0,
        id: 208660668,
        publishTime: '1476340202000',
        recommendedEffectiveCandidates: 2,
        effectiveCandidates: 6,
        uniqueImpressions: 0 },
     isApplied: null,
     engagement: null,
     recno: 208660668,
     title: 'Filipino Blog/Content Writers For a Filipino Site (Using WordPress)',
     freelancersToHire: 3,
     maxAmount: { amount: 0, currencyCode: 'USD' },
     duration: null,
     subcategory2: 'Article & Blog Writing',
     sticky: false,
     stickyLabel: '',
     feedback: null },
  id: 57ffcdbf717ca50cf0b4cbc4 }

我拥有的foreach 似乎遍历了每个项目,但我无法获得每个项目的标题。有什么帮助吗?

【问题讨论】:

  • 那不是 JSON.. 你可以得到原始 JSON 数据,如果没有你可以fs.writeFileSync('somefile.json', JSON.stringify(feed))
  • @Keith 我仍然无法获取每个对象的标题...您知道其他方法吗?
  • 这就是提要对象的样子吗?它看起来不像一个数组,而是四个独立的对象。运行 JSON.stringify(feeds) 时会显示什么?
  • 它看起来像这样:[{"_id":"57ffcd9e717ca50cf0b4cb61","data":{"feedback":null,"stickyLabel":"","sticky":false, "subcategory2":"Web Development","duration":"不到 1 个月","maxAmount":{"currencyCode":"USD","amount":0},"freelancersToHire....
  • 它是这样工作的,我不知道为什么: var list = JSON.parse(JSON.stringify(feeds)); _.forEach(list, function (row, key) { console.log(row.data.title); });

标签: javascript json node.js


【解决方案1】:

我不太确定您要做什么,但是如果数据确实是您的 cmets 上的数据,则类似的方法应该可以工作:

feeds.forEach(function(feed){
  console.log(feed.data.title);
});

如果您想要更快的性能,请使用常规 for 循环。我在我的项目中对此进行了测试。如果这不起作用,请告诉我。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    相关资源
    最近更新 更多