【问题标题】:How to delete a colon : after my json.stringify如何删除冒号:在我的 json.stringify 之后
【发布时间】:2020-07-15 11:02:40
【问题描述】:

我在使用 json.stringify 时遇到问题,它返回一个 json,在 json 后面有一个冒号

axios.post('http://54.196.61.131/api/v0/user/250/product/add/scan',
        JSON.stringify({
      
          name: currentProduct.product.product_name_fr,
          brand: currentProduct.product.brands,
          image: currentProduct.product.image_front_thumb_url,
          product_quantity: currentProduct.product.quantity,
          ingredients: currentProduct.product.ingredients_text,
          quantity: parseInt(quantite, 10),
          nutriscore_grade: currentProduct.product.nutriscore_grade,
          expiration_date: expDate,
          barcode: currentProduct.code,
        }))

它最后返回一个 :,所以我的 API 出现 500 错误:

{"name":"Farine de blé suprême","brand":"Francine","image":"https://static.openfoodfacts.org/images/products/306/811/070/3232/front_fr.37.100.jpg","product_quantity":"1 kg","ingredients":"Farine de blé type 45.","quantity":6,"nutriscore_grade":"a","barcode":"3068110703232","expiration_date":"2020-07-24T10:41:04.000Z"}: 

谢谢

【问题讨论】:

  • 不,不会,因为这不是有效的 JSON。请添加显示实际问题的minimal reproducible example(最好是runnable snippet <>)。
  • 尝试控制台记录数据,它是字符串化的形式,通常在字符串化结束后你永远不会得到冒号。

标签: javascript json reactjs


【解决方案1】:

尝试序列化您的数据。

serialize = function (obj) {
    var str = [];
    for (var p in obj)
        if (obj.hasOwnProperty(p)) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
    return str.join("&");
}
axios.post('http://54.196.61.131/api/v0/user/250/product/add/scan', serialize({
    // Your JSON data
})

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多