我怎样才能得到一个JSON文件,这样我就可以在我的原生JavaScript文件中修改/做一些事情?

i2byvkas  于 4个月前  发布在  Java
关注(0)|答案(1)|浏览(63)

此问题在此处已有答案

How do I load a JSON object from a file with ajax?(4个答案)
What is the difference between client-side and server-side programming?(3个答案)
上个月关门了。
我目前正在学习如何保存数据和属性,我想这包括学习JSON,但为了真正使用JSON,我需要一个文件。

问题:如何引用JSON文件,以便从中提取值并进行数据...操作?

注意:我现在只需要帮助引用JSON文件,我还不需要帮助学习JSON。
注意事项:我使用的是本地语言,我没有使用jQuery(我知道人们在十年的大部分时间里都没有使用它,但我已经看到了使用jQuery解决这个问题的方法)。我看到的所有其他帖子都使用框架解决了这个问题,但没有使用原生JS和HTML。
注意:我知道“数据保存”是模糊的,但我还不知道解释这些事情的术语和语言。你会在我的JavaScript中看到我的意思。
注意:如果有区别的话,我也会在vsCode的浏览器版本上这样做。
超文本标记语言:

<!DOCTYPE html>
<html lang="en">

<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible">
        <meta name="viewport", content="width=device-width, initial-scale=1.0">
        <meta name="author" content="Christian Davis">
        <link rel="stylesheet" href="styles.css">

        <title>Data stuff and JSON</title>
    </head>

    <body>
        <p id="myConsole">&gt;&nbsp;<span id="myMessage"></span></p>

        <script src="app.js"></script>
    </body>
</html>

字符串
请不要介意这些乱七八糟的东西,这些JavaScript只是我的笔记。
JavaScript:

function logMessage(msg) {
    document.querySelector('#myMessage').textContent += msg + ". ";
}

//This creates one person with the attributes/properties displayed below.
//These attributes/properties can be got by objectName.propertyName or with objectName["propertyName"]
const Person = {
    firstName: "John",
    lastName: "Doe",
    age: "Unknown",
    occupation: "Unknown",
    fullName: function() {
        return this.firstName + " " + this.lastName;
    }
}

//This returns the first and last name we are using here
let personFullName = Person.fullName();

//logMessage(personFullName);
//logMessage(Person.occupation + ", " + Person.age);

//Creates a class that multiple things can be made from.
//Kind of like what is above.
class Car {
    constructor({position, velocity, weight, acceleration, make, model, color, length, width, height}) {
        this.position = position;
        this.velocity = velocity;
        this.weight = weight;
        this.acceleration = acceleration;
        this.make = make;
        this.model = model;
        this.color = color;
        this.length = length;
        this.width = width;
        this.height = height;
    }
}

//car is a Car with specific elements
const car = new Car ({
    position: {
        x: 176,
        y: 41
    },
    velocity: {
        x: 0,
        y: 0
    },
    weight: 3570,
    acceleration: 10,
    make: "Toyota",
    model: "Avalon",
    color: "White",
    length: 195.9,
    width: 72.8,
    height: 56.5
})

//logMessage(car.position.x);
//logMessage(car.weight);
//logMessage(car.model)

//This is a text version of JSON. This is one way to do it.
let text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}'

//This turns all of the text above into an array, I think
let object = JSON.parse(text);
let randomName = object.employees[0].firstName + " " + object.employees[2].lastName;


JSON(objects.json):

{
    "Team Members" : [
        {
            "firstName":"John",
            "lastName":"Doe",
            "employeeCode":"2219"
        }, 
        {
            "firstName":"Jane",
            "lastName":"Smith",
            "employeeCode":"6340"
        }
    ]
}


我想我是希望能够在JS文件中执行JSON,但后来我了解到这是它自己的事情。

jk9hmnmh

jk9hmnmh1#

您可以使用Fetch API加载JSON文件:

async function fetchJSON() {
  try {
    const response = await fetch('./objects.json');
    const data = await response.json();

    // DO STUFF TO YOUR JSON HERE

  } catch (error) {
    // ...
  }
}

字符串

相关问题