簡體   English   中英

如何左連接兩個外部 JSON url

[英]How to LEFT JOIN two external JSON url

我正在嘗試使用外部 JSON 完成以下結果。 我在谷歌和stackverflow上搜索,我找不到任何答案。 誰可以幫我這個事? 先感謝您。

people = 
[{id: 1, name: "Tom", carid: 1},
{id: 2, name: "Bob", carid: 1},
{id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}];

cars=
[{id: 1, name: "Ford Fiesta", color: "blue"},
{id: 2, name: "Ferrari", color: "red"},
{id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}];

var res = alasql('SELECT people.name AS person_name, cars.name, cars.color \
    FROM ? people LEFT JOIN ? cars ON people.carid = cars.id',[people, cars]);

結果:

[{"person_name":"Tom","name":"Ford Fiesta","color":"blue"},{"person_name":"Bob","name":"Ford Fiesta","color":"blue"},{"person_name":"Sir Benjamin Rogan-Josh IV","name":"Ferrari","color":"red"}]

這是我訪問外部 JSON 的代碼:

people.json

[{id: 1, name: "Tom", carid: 1},{id: 2, name: "Bob", carid: 1}, {id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}]

汽車.json

[{id: 1, name: "Ford Fiesta", color: "blue"},{id: 2, name: "Ferrari", color: "red"},{id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}]

JS代碼

$(document).ready(function() { 
         $.getJSON('http://example.com/people.json', function(data) { 
                    people = JSON.parse(data);
                }); 

         $.getJSON('http://example.com/cars.json', function(data) { 
                    cars = JSON.parse(data);
                }); 


        var res = alasql('SELECT people.name AS person_name, cars.name, cars.color \
        FROM ? people LEFT JOIN ? cars ON people.carid = cars.id',[people, cars]);

        document.getElementById('id01').textContent = JSON.stringify(res);
}):

這是我在控制台中遇到的錯誤

Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at Object.success (list.html:26)
at c (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at l (jquery.min.js:2)
at XMLHttpRequest.<anonymous> (jquery.min.js:2)

只需遍歷每個人並找到對應的汽車詳細信息,這樣您就可以完成您的任務。

請使用簡單for of找到下面的工作代碼。 您也可以使用map運算符

 people = [{ id: 1, name: "Tom", carid: 1 }, { id: 2, name: "Bob", carid: 1 }, { id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2 } ]; cars = [{ id: 1, name: "Ford Fiesta", color: "blue" }, { id: 2, name: "Ferrari", color: "red" }, { id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow" } ]; const result = []; for (const peop of people) { const car = cars.filter(d => d.id === peop.carid)[0]; result.push({ person_name: peop.name, name: car.name, color: car.color }); } console.log(result)

我找到了答案。

 var requestpeop = new XMLHttpRequest();
    requestpeop.open('GET', 'people.json', false);  // `false` makes the request synchronous
    requestpeop.send(null);

    if (requestpeop.status === 200) {// That's HTTP for 'ok'
      var people =  JSON.parse(requestpeop.responseText);
    }

    var requestcar = new XMLHttpRequest();
    requestcar.open('GET', 'car.json', false);  // `false` makes the request synchronous
    requestcar.send(null);

    if (requestcar.status === 200) {// That's HTTP for 'ok'
      var cars = JSON.parse(requestcar.responseText);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM