簡體   English   中英

我需要以這種格式插入 json "[{\"id\":1,\"name\":\"Eat Fresh\",\"app_type\":1,\"price\":\"500\ "}]" 但我有一個這種格式的數組

[英]i need to insert json in this format "[{\"id\":1,\"name\":\"Eat Fresh\",\"app_type\":1,\"price\":\"500\"}]" but i have a array in this format

  this.additional_app_data.forEach((element:any) => {
    // console.log("the name is",element.name);
    // arr.push(element.id);
    // arr1.id = element.id
    // arr1.name = element.name
    // arr1.app_type = element.app_type
    // arr1.price = element.price
    // console.log("the array is ",arr1);
    const obj = {
      "id": element.id,
      "name": element.name,
      "app_type":element.app_type,
      "price":element.price

    };


  console.log("The object values are ",obj);

我有這個格式 {id: 1, name: 'Restaurant Partner App', app_type: 2, price: 10} 如果我提交這個,它會在 angular 中給出 json 錯誤任何人都可以幫助轉換為 json 錯誤任何人都可以幫助轉換為所需的 json 錯誤

您有一個具有唯一 obj 的數組,因此您可以使用 this.additional_app_data[0] 來獲取數組的唯一元素。 轉換字符串中的數字,您只需使用,例如"id":''+element.id (您在值之前添加一個空字符串)。 將值轉換為字符串但您使用的數字,例如myNumber=+myvariableString

所以我想你想做(但我真的不太確定我理解你)

const obj = {
  "id": ''+element.id,
  "name": element.name,
  "app_type":''+element.app_type,
  "price":''+element.price
};

暫無
暫無

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

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