“json ke formdata” Kode Jawaban

Formdata ke json

var object = {};
formData.forEach(function(value, key){
    object[key] = value;
});
var json = JSON.stringify(object);

// Or ES6 arrow function
var object = {};
formData.forEach((value, key) => object[key] = value);
var json = JSON.stringify(object);
yusuf_fazeri

json ke formdata

function jsonToFormData(json){
    let formData = new FormData();
    if(!json) return formData;
    try{
        json = JSON.parse(json);
    }finally{}
    if(typeof json != 'object'||Array.isArray(json)) return formData;
    for(key in json)
        formData.append(key,typeof json[key] == 'object' ? JSON.stringify(json[key]) : json[key]);

    return formData;
}
2589

json ke formdata

function buildFormData(formData, data, parentKey) {
  if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) {
    Object.keys(data).forEach(key => {
      buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key);
    });
  } else {
    const value = data == null ? '' : data;

    formData.append(parentKey, value);
  }
}

function jsonToFormData(data) {
  const formData = new FormData();
  
  buildFormData(formData, data);
  
  return formData;
}

const my_data = {
  num: 1,
  falseBool: false,
  trueBool: true,
  empty: '',
  und: undefined,
  nullable: null,
  date: new Date(),
  name: 'str',
  another_object: {
    name: 'my_name',
    value: 'whatever'
  },
  array: [
    {
      key1: {
        name: 'key1'
      }
    }
  ]
};

jsonToFormData(my_data)
Smiling Swan

Cara mengonversi formdata (objek html5) ke json

var object = {};
formData.forEach(function(value, key){
    object[key] = value;
});
var json = JSON.stringify(object);
Bug Killer

Jawaban yang mirip dengan “json ke formdata”

Pertanyaan yang mirip dengan “json ke formdata”

Lebih banyak jawaban terkait untuk “json ke formdata” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya