是否可以在json中进行表达式/计算? [英] Is it possible to do expressions/calculations in json?

查看:514
本文介绍了是否可以在json中进行表达式/计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用出色的json服务器作为应用程序的后端,对于击中自定义端点以检索一些数据确实很有用.但是,如果它允许我进行计算/表达式以使我也可以模仿该后端的行为,那将是超级有用的.

I am using the wonderful json-server as the backend of my application and it's really useful for hitting custom endpoints to retrieve some data. but what would be super useful if it allowed me to do calculations/expression so that i can mimic that backend behaviour too.

以这种数据结构为例

{
  "products": [
    {
      "name": "football",
      "id": "SPO-001",
      "category": "sport",
      "price": 40,
      "couponApplied": "false",
      "coupons": [
        "daw124qdw",
        "a1212cxn"
      ]
    }
  ]
}

我想要某种表达方式,例如"discountPrice": couponApplied ? price * couponDiscount

I would like some way of saying something like "discountPrice": couponApplied ? price * couponDiscount

那只是我的伪编码.但我想做些可以即时计算价格的事情.或在我发出请求时进行计算并向我返回计算出的数据(就像后端应用程序一样)

that's just me pseudo coding. but I would like to do something where I can calculate the price on the fly. or when I make a request it does a calculation and returns me the calculated data (like a backend app would)

我了解我可以提出请求,申请优惠券并提供新价格.甚至发帖要求并更改价格.但这一切都是在客户端完成的.有没有办法用json或json-server或任何其他解决方案来做到这一点.那有道理吗?

I understand I can make a request, apply the coupon and render that new price. or even make a post request and change the price. but that is all done client side. is there any way to do this either with json or json-server or any other solutions. if that makes sense?

推荐答案

JSON表示JavaScript Object Notation,它是数据结构,并且没有任何预处理器.您可以使用任何JSON解析器并动态添加/更改所需的值.

JSON means JavaScript Object Notation and is data structure, and does not have any preprocessor for it. You can use any JSON parser and append/change values that you need dynamically.

因此,简而言之:不,不可能添加动态值

So in short: no, there is no possibility to add dynamic values

这篇关于是否可以在json中进行表达式/计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆