从JSON数组解析两个对象

时间:2020-03-13 14:15:44

标签: javascript arrays node.js json

所以我考虑过使用Jquery和getJSON,但我想不通如何使它工作,基本上我想要实现的是获取['statename']和['city ']。

我想知道什么是最简单的获取方式,然后将每个结果保存到['statename']和['city']

这样,我可以在URL中以$ {statename}和$ {city}的形式对其进行回调。

这是我到目前为止的代码:

scripts.js

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        const {latitude, longitude} = position.coords;
        let pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      const currentLocation = `https://geocode.xyz/${latitude},${longitude}?json=1`
      console.log(currentLocation)
    })};

scrapper.js

debug = require ('../models/conn');
const puppeteer = require('puppeteer');
const axios = require('axios');
const cheerio = require('cheerio');

async function searchJobs(i) {
   const url = await axios.get('https://indeed.com/jobs?q=Web+Developer&l=Atlanta&fromage=last')
    // return fetch(`${url}${i}`)
        .then(response =>  response)

        .then(res => {
            const jobs = [];
            const $ = cheerio.load(res.data);

            $('.result').each((index, element) => {
                const title = $(element).children('.title').text();
                const linkToJob = $(element).children('.title').children('a').attr('href')
                const body = $(element).children('.summary').text();
                jobs[index] = { title, linkToJob, body };
            });
            console.log(jobs);
            return jobs;
            // Prints tbe second child of results class results for that page in console.
            //    console.log($('.result').children().eq(1).text());
        });
        return url;
};


// async function userCity(lat, long){
//         const currentLocation = `https://geocode.xyz/${lat},${long}?json=1`
//         await axios.get(currentLocation).then(response => {
//             console.log(response['city'], response['statename']);
//         })
//     }
module.exports = searchJobs;

如果您需要或对查看文件感到疑惑,我认为这是与该问题真正相关的仅有两个文件..谢谢:)

如果我要接受不赞成票,请至少让我知道为什么

1 个答案:

答案 0 :(得分:0)

使用以下代码修复它。.

    $.getJSON(`https://geocode.xyz/${latitude},${longitude}?json=1`, function (data) {
        let state = `${data.statename}`
        let city = `${data.city}`
        console.log(state)
        console.log(city)
    });