12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- export function parseXML(xmlData) {
- // 提取XML中的JSON字符串
- const jsonString = xmlData.replace('<?xml version="1.0" encoding="utf-8"?>', '').replace(
- '<string xmlns="http://schema.kingdee.com/LUTAIWebServiceExtend/">', '').replace('</string>', '');
- // 将JSON字符串转换为JSON对象
- try {
- const jsonData = JSON.parse(jsonString);
- // console.log(jsonData); // 输出解析后的JSON数据
- return jsonData
- } catch (e) {
- console.error('解析JSON时发生错误:', e);
- }
- }
- export function parseEncodedData(jsonData) {
- // let formData = [];
- // for (let key in jsonData) {
- // if (Array.isArray(jsonData[key])) {
- // jsonData[key].forEach((item, index) => {
- // for (let subKey in item) {
- // formData.push(encodeURIComponent(`${key}[${index}][${subKey}]`) + '=' + encodeURIComponent(
- // item[subKey]));
- // }
- // });
- // } else {
- // formData.push(encodeURIComponent(key) + '=' + encodeURIComponent(jsonData[key]));
- // }
- // }
- // let str = formData.join('&');
- // console.log(str)
- // return str;
- const urlEncodedData = jsonToUrlEncoded(jsonData).join('&');
- console.log(urlEncodedData)
- return urlEncodedData;
- }
- function jsonToUrlEncoded(jsonData, prefix = 'jsonData') {
- let str = [];
- for (let key in jsonData) {
- if (jsonData.hasOwnProperty(key)) {
- let encodedKey = encodeURIComponent(prefix ? `${prefix}[${key}]` : key);
- let value = jsonData[key];
- if (Array.isArray(value)) {
- value.forEach((item, index) => {
- str.push(...jsonToUrlEncoded(item, `${encodedKey}[${index}]`));
- });
- } else if (typeof value === 'object' && value !== null) {
- str.push(...jsonToUrlEncoded(value, encodedKey));
- } else {
- let encodedValue = encodeURIComponent(value);
- str.push(encodedKey + '=' + encodedValue);
- }
- }
- }
- return str;
- }
|