使用where语句从JSON中检索数据(Retrieve data from JSON with where statement)

编程入门 行业动态 更新时间:2024-10-15 08:27:17
使用where语句从JSON中检索数据(Retrieve data from JSON with where statement)

我有一个像这样的结构的json文件:

{ "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.379" }, { "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.478" }

所以,我的问题是如何检索fuelTypeID=1的fuelPrice ?

我的JavaScript文件是这样的:

for (i = 0; i < obj.length; i++) { var _gasStationCompName = obj[i].fuelCompNormalName; var priceID = obj[i].fuelTypeID; switch (fuelId) { case '1': case priceID == 1: price = obj[i].fuelPrice; break; case '2': case priceID == 2: price = obj[i].fuelPrice; break; default: price = "0"; }

我的问题是,在price = obj[i].fuelPrice仅显示fuelType=2的fuelPrice 。 我怎么能在switch语句里面过滤结果?

在此之前,我正在解析JSON文件。 此外,你可以看到我有case: '1' 。 这是因为我在我的HTML文件中有这样的内容:

<div> <select onChange="getData(this.value);"> <option value="1">Gas</option> <option value="2">Diesel</option> </select> </div>

I have a json file with a structure like this:

{ "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.379" }, { "gasStationID":"441", "gasStationLat":"39.6337420", "gasStationLong":"22.4324412", "fuelCompID":"6", "fuelCompNormalName":"AVIN", "ddID":"42010100", "municipalityID":"42010000", "countyID":"42000000", "phone1":null, "username":"user1", "fuelTypeID":"1", "fuelPrice":"1.478" }

So, my question is how to retrieve the fuelPrice with fuelTypeID=1?

My JavaScript file is this:

for (i = 0; i < obj.length; i++) { var _gasStationCompName = obj[i].fuelCompNormalName; var priceID = obj[i].fuelTypeID; switch (fuelId) { case '1': case priceID == 1: price = obj[i].fuelPrice; break; case '2': case priceID == 2: price = obj[i].fuelPrice; break; default: price = "0"; }

My problem is that in the price = obj[i].fuelPrice is showing only the fuelPrice with fuelType=2. How can I filter inside the switch statement the result?

Before that I'm parsing the JSON file. Also, you can see that I have case: '1'. That's because I have something like this in my HTML file:

<div> <select onChange="getData(this.value);"> <option value="1">Gas</option> <option value="2">Diesel</option> </select> </div>

最满意答案

变量和对象键区分大小写(A!= a);

for (i = 0; i < obj.length; i++) { var _gasStationCompName = obj[i].fuelCompNormalName; var priceID = obj[i].fuelTypeID; // fuelTypeId => fuelTypeID switch (priceID) { fuelTypeId // => priceID case '1': case priceID == 1: price = obj[i].fuelPrice; break; case '2': case priceID == 2: price = obj[i].fuelPrice; break; default: price = "0"; } }

I managed to find the solution. Here is the right switch statement that works like a charm:

var priceID = obj[i].fuelTypeID; switch (fuelTypeID) { case '1': if (priceID == 1) { price = obj[i].fuelPrice; } break; case '2': if (priceID == 2) { price = obj[i].fuelPrice; } break; default: price = 0; }

更多推荐

本文发布于:2023-07-15 17:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   数据   JSON   statement   Retrieve

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!